在PHP中添加7天会错误地计算时间

时间:2017-02-01 15:44:09

标签: php strtotime

当我这样做时:

root

我收到了这个输出:

$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)));
}

知道为什么+ 7天短缺12小时?

2 个答案:

答案 0 :(得分:3)

有区别
  1. 显示小时01到12

    date('h'); 
    
  2. 显示小时00到23

    date('H');
    
  3. 有关它的更多信息: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)));
    }
    

答案 1 :(得分:0)

不是,你用以下方式显示它:

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