Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP strotime在闰年缺少一天_Php_Date_Strtotime_Leap Year - Fatal编程技术网

PHP strotime在闰年缺少一天

PHP strotime在闰年缺少一天,php,date,strtotime,leap-year,Php,Date,Strtotime,Leap Year,我试图通过使用strotime在第一个日期后添加13天来生成连续日期周期的列表,如下所示: $start = strtotime(date('2020-06-01')); for($x = 1; $x<=10; $x++) // increment $x until 10 { $period = $start - ($x * 1209600); // 1209600 is equivalent to 14 days. Multiply by $x $period_

我试图通过使用strotime在第一个日期后添加13天来生成连续日期周期的列表,如下所示:

$start = strtotime(date('2020-06-01'));

for($x = 1; $x<=10; $x++) // increment $x until 10
{
    $period     = $start - ($x * 1209600); // 1209600 is equivalent to 14 days. Multiply by $x
    $period_end = date('Y-m-d', strtotime(date('Y-m-d', $period). ' + 13 days'));
    echo date('Y-m-d', $period) . " - " . $period_end ."<br>";
}
2020-05-18 - 20-05-31
2020-05-04 - 20-05-17
2020-04-20 - 20-05-03
2020-04-06 - 20-04-19
2020-03-23 - 20-04-05
2020-03-09 - 20-03-22
2020-02-23 - 20-03-07
2020-02-09 - 20-02-22
2020-01-26 - 20-02-08
2020-01-12 - 20-01-25
在达到“2020-02-23-20-03-07”范围之前,一切都按预期运行。应报告“2020-02-24-2020-03-08” 为什么换班1天?这是PHP strotime中与闰年相关的错误吗


编辑:这不是闰年问题。在我的时区,这是一个夏令时问题。当DST发生在3/8时,从epoch开始以秒为单位的时间改变了一小时。这将我的日期提前了一个小时,这是我的前一天。

如果我们将
H:i
添加到你的日期(),一切都会变得清晰

$start = strtotime(date('2020-06-01'));

for($x = 1; $x<=10; $x++) // increment $x until 10
{
    $period     = $start - ($x * 1209600); // 1209600 is equivalent to 14 days. Multiply by $x
    $period_end = date('Y-m-d H:i', strtotime(date('Y-m-d H:i', $period). ' + 13 days'));
    echo date('Y-m-d H:i', $period) . " - " . $period_end ."<br>\n";
}

无法复制。也许您可以在php.ini中检查时区设置,并将它们添加到您的问题中。将开始日期更改为
2020-06-01 12:00:00
可能有助于避免夏令时之类的问题。处理此类日期时,应使用
DateTime()~、
DateInterval()
DatePeriod()
strotime()
不是为处理这类事情而设计的。捕捉得好!我将$start的值更改为'2020-06-01 12:00:00',并观看了H:I从12更改为11。非常感谢。我甚至没有想到DST。
2020-05-18 00:00 - 2020-05-31 00:00<br>
2020-05-04 00:00 - 2020-05-17 00:00<br>
2020-04-20 00:00 - 2020-05-03 00:00<br>
2020-04-06 00:00 - 2020-04-19 00:00<br>
2020-03-22 23:00 - 2020-04-04 23:00<br>
2020-03-08 23:00 - 2020-03-21 23:00<br>
2020-02-23 23:00 - 2020-03-07 23:00<br>
2020-02-09 23:00 - 2020-02-22 23:00<br>
2020-01-26 23:00 - 2020-02-08 23:00<br>
2020-01-12 23:00 - 2020-01-25 23:00<br>
$start = strtotime(date('2020-06-01 12:00'));