duanqiang3925 2014-08-23 09:47
浏览 158
已采纳

PHP DateTime :: format('I')不准确的bug?

I'm wondering if this is a bug, or if I missed something.

While creating a method to isolate the timestamp when DST switches from on to off and vice versa, I noticed a potential bug with DateTime::format('I'). When DST ends and the time is switched backwards one hour, DateTime::format('I') reports the switch one hour too late.

In the 'America/Montreal' timezone, we can look at these timestamps when DST ends:

1414904400, Sun, 02 Nov 2014 01:00:00 -0400 (DST on)
1414908000, Sun, 02 Nov 2014 01:00:00 -0500 (DST off)
1414911600, Sun, 02 Nov 2014 02:00:00 -0500 (DST off)

Notice the DST ends on the second timestamp when 01:00:00 is repeated.

But, using DateTime::format('I') the ending is reported on the third timestamp.

$timezone = new DateTimezone('America/Montreal');
$datetime = new DateTime('now', $timezone);
$datetime->setTimestamp(1414904400);
echo $datetime->format('I');
$datetime->setTimestamp(1414908000);
echo $datetime->format('I');
$datetime->setTimestamp(1414911600);
echo $datetime->format('I');

returns 110 (should be 100)

If we use date('I') we get the correct answer.

date_default_timezone_set('America/Montreal');
echo date('I', 1414904400);
echo date('I', 1414908000);
echo date('I', 1414911600);

returns 100 (as expected)


When DST begins DateTime::format('I') works fine, it only seems to fail when DST ends.

Also, I have tested this for a few different years in a few different timezones, but I have no idea of the depth of this potential bug. Much more testing is needed.

Anyone know what this is?

  • 写回答

1条回答 默认 最新

  • dongmei9203 2014-08-23 17:33
    关注

    While creating a method to isolate the timestamp when DST switches from on to off and vice versa ...

    That method already exists as DateTimeZone::GetTransitions. Documentation here.

    You may also be interested in the related problem of determining whether a particular date/time is within a transition period, which is described in this answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置