PHP DateTime Timedifference仅在UTC时区正确吗? [英] PHP DateTime Timedifference only correct in UTC Timezone?

查看:72
本文介绍了PHP DateTime Timedifference仅在UTC时区正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用以下行为解释我吗:

Can anybody explain me following behaviours:

$end = new DateTime("2015-03-01", new DateTimeZone("Europe/Berlin"));
$start = new DateTime("2015-02-01", new DateTimeZone("Europe/Berlin"));
$diff = $start->diff($end);
//result: DateInterval Object ( [y] => 0 [m] => 0 [d] => 28 [h] => 0 [i] => 0 [s] => 0 [invert] => 0 [days] => 28 ) 

我不使用时区或欧洲/柏林,所以我得到了错误的时差。 28天的确正确,但是在这种情况下,这是一个月。

If I don't use a timezone or Europe/Berlin I get wrong time diffs. 28 days is indeed correct, but in this case it's a month.

$end = new DateTime("2015-03-01", new DateTimeZone("UTC"));
$start = new DateTime("2015-02-01", new DateTimeZone("UTC"));
$diff = $start->diff($end);
//result: DateInterval Object ( [y] => 0 [m] => 1 [d] => 0 [h] => 0 [i] =>; 0 [s] => 0 [invert] => 0 [days] => 28 )

它有效

$end = new DateTime("2015-04-01", new DateTimeZone("Europe/Berlin"));
$start = new DateTime("2015-03-01", new DateTimeZone("Europe/Berlin"));
$diff = $start->diff($end);
//result: DateInterval Object ( [y] => 0 [m] => 1 [d] => 3 [h] => 0 [i] => 0 [s] => 0 [invert] => 0 [days] => 31 ) 

1个月, 3天?!? 2月,您在这里吓到吗?

1 month and 3 days?!? February, are you spooking here?

$end = new DateTime("2015-04-01", new DateTimeZone("UTC"));
$start = new DateTime("2015-03-01", new DateTimeZone("UTC"));
$diff = $start->diff($end);
//result: DateInterval Object ( [y] => 0 [m] => 1 [d] => 0 [h] => 0 [i] =>; 0 [s] => 0 [invert] => 0 [days] => 31 )

31天= 1个月,UTC是正确的

31 days = 1 month, UTC is correct

$end = new DateTime("2015-05-01", new DateTimeZone("Europe/Berlin"));
$start = new DateTime("2015-04-01", new DateTimeZone("Europe/Berlin"));
$diff = $start->diff($end);
//result: DateInterval Object ( [y] => 0 [m] => 0 [d] => 30 [h] => 0 [i] => 0 [s] => 0 [invert] => 0 [days] => 30 ) 

30天是

$end = new DateTime("2015-05-01", new DateTimeZone("UTC"));
$start = new DateTime("2015-04-01", new DateTimeZone("UTC"));
$diff = $start->diff($end);
//result: DateInterval Object ( [y] => 0 [m] => 1 [d] => 0 [h] => 0 [i] =>; 0 [s] => 0 [invert] => 0 [days] => 30 )

UTC正确

$end = new DateTime("2015-06-01", new DateTimeZone("Europe/Berlin"));
$start = new DateTime("2015-05-01", new DateTimeZone("Europe/Berlin"));
$diff = $start->diff($end);
//result: DateInterval Object ( [y] => 0 [m] => 1 [d] => 1 [h] => 0 [i] => 0 [s] => 0 [invert] => 0 [days] => 31 ) 

还是31天,正确的一个月,但为什么要+1天?

again 31 days, correct one month, but why +1 day?

$end = new DateTime("2015-06-01", new DateTimeZone("UTC"));
$start = new DateTime("2015-05-01", new DateTimeZone("UTC"));
$diff = $start->diff($end);
//result: DateInterval Object ( [y] => 0 [m] => 1 [d] => 0 [h] => 0 [i] =>; 0 [s] => 0 [invert] => 0 [days] => 31 )

UTC仍然正确

我不明白。谢谢您的帮助。

I don't get it. Thanks for your help.

推荐答案

我相信您遇到了一个已知错误。

I believe you are experiencing a known bug.

请参阅:

  • PHP::Bug #52480 Incorrect difference using DateInterval

这篇关于PHP DateTime Timedifference仅在UTC时区正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆