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 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致