谁能解释一下我的行为:
$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天确实是正确的,但在这种情况下是一个月。
$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 )
与UTC,它工作
$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天?二月,你在这里闹鬼吗?
$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正确
$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个月,但为什么是+1天?
$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仍然正确
我不明白。谢谢你的帮助。
我相信你遇到了一个已知的错误。
:
- PHP::Bug #52480使用DateInterval不正确