【发布时间】:2023-03-07 06:29:01
【问题描述】:
我正在尝试通过使用 strtotime 将 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 strtotime 中与闰年相关的错误吗?
编辑:这不是闰年问题。事实证明,这是我所在时区的夏令时问题。当 DST 发生在 3/8 时,从纪元开始以秒为单位的时间改变了一个小时。这将我的日期提前 1 小时,最终成为前一天。
【问题讨论】:
-
无法重现。也许您可以检查 php.ini 中的时区设置,并将它们添加到您的问题中。将开始日期更改为
2020-06-01 12:00:00可能会有所帮助,以避免出现夏令时等问题。 -
在处理这样的日期时,您应该使用
DateTime()~,DateInterval(), andDatePeriod()`。strtotime()不是为处理这类事情而设计的。
标签: php date strtotime leap-year