在 PHP 中添加 7 天计算的时间不正确
Adding 7 days in PHP calculates time incorrectly
当我这样做时:
$date = "2017-02-06 21:30:00";
$end_date = "2017-02-28 21:30:00";
while (strtotime($date) <= strtotime($end_date)) {
echo $date."<br />";
$date = date("Y-m-d h:i:s", strtotime("+7 day", strtotime($date)));
}
我得到这个输出:
2017-02-06 21:30:00
2017-02-13 09:30:00
2017-02-20 09:30:00
2017-02-27 09:30:00
知道为什么 + 7 天少了 12 小时吗?
有差异
显示 01 到 12 小时
date('h');
显示 00 点到 23 点
date('H');
更多信息:http://php.net/manual/en/function.date.php
您的代码应该是:
$date = "2017-02-06 21:30:00";
$end_date = "2017-02-28 21:30:00";
while (strtotime($date) <= strtotime($end_date)) {
echo $date."<br />";
$date = date("Y-m-d H:i:s", strtotime("+7 day", strtotime($date)));
}
不是,您显示的是:
date("Y-m-d h:i:s",$time); // as h stands for 12-hour format of an hour with leading zeros 01 through 12
改用 H :
date("Y-m-d H:i:s",$time); // as H stands for 24-hour format of an hour with leading zeros 00 through 23
当我这样做时:
$date = "2017-02-06 21:30:00";
$end_date = "2017-02-28 21:30:00";
while (strtotime($date) <= strtotime($end_date)) {
echo $date."<br />";
$date = date("Y-m-d h:i:s", strtotime("+7 day", strtotime($date)));
}
我得到这个输出:
2017-02-06 21:30:00
2017-02-13 09:30:00
2017-02-20 09:30:00
2017-02-27 09:30:00
知道为什么 + 7 天少了 12 小时吗?
有差异
显示 01 到 12 小时
date('h');
显示 00 点到 23 点
date('H');
更多信息:http://php.net/manual/en/function.date.php
您的代码应该是:
$date = "2017-02-06 21:30:00";
$end_date = "2017-02-28 21:30:00";
while (strtotime($date) <= strtotime($end_date)) {
echo $date."<br />";
$date = date("Y-m-d H:i:s", strtotime("+7 day", strtotime($date)));
}
不是,您显示的是:
date("Y-m-d h:i:s",$time); // as h stands for 12-hour format of an hour with leading zeros 01 through 12
改用 H :
date("Y-m-d H:i:s",$time); // as H stands for 24-hour format of an hour with leading zeros 00 through 23