DateTime::diff 和夏令时结束

DateTime::diff and end of Daylight Saving Time

上周末夏令时结束,时钟向后拨了 1 小时。当比较日期处于不同时间时,我发现 DateTime::diff() 有一个奇怪的行为:spring 时间和冬季时间。

new DateTime('2015-10-28 12:50:00')->diff(new DateTime('2015-10-19 13:20:00'))

result = {DateInterval} [15]
    y = 0
    m = 0
    d = 9
    h = -1   # negative hour!
    i = 30
    s = 0

当相差超过一小时时一切正常:

new DateTime('2015-10-28 12:50:00')->diff(new DateTime('2015-10-19 14:20:00'))

result = {DateInterval} [15]
    y = 0
    m = 0
    d = 8
    h = 22
    i = 30
    s = 0

有什么解决办法吗?这是 PHP 中的错误吗?

是的,这是一个错误 PHP 目前不处理 DST 转换。错误报告 #51051 (still open) and #55253 (fixed in PHP 5.3.9) describe the problems you're having.Best practice is to do all your date calculations in UTC, which avoids this problem. See this post 以获取更多详细信息