2012-12-04 46 views
0

有人可以解釋我這個'邏輯'嗎?php date_diff瘋狂

$datetime1 = date_create('2012-04-01'); 
$datetime2 = date_create('2012-05-01'); 
$interval = date_diff($datetime1, $datetime2); 
print_r($interval); 

返回0米(月)和30天(天)。但是:

$datetime1 = date_create('2012-05-01'); 
$datetime2 = date_create('2012-06-01'); 
$interval = date_diff($datetime1, $datetime2); 
print_r($interval); 

返回1m(月)和1d(天)。

(PHP 5.3.15,LC_ALL EN_US)

UPDATE

我用php_value date.timezone歐洲/阿姆斯特丹這似乎賺取差價。仍然認爲這是奇怪的,東部時間或不,它應該只給1個月。對?

http://sandbox.onlinephpfunctions.com/code/f28914a13f4047c79a19bae70570029a39196148

+0

我get:DateInterval Object([y] => 0 [m] => 1 [d] => 0 [h] => 0 [i] => 0 [s] => 0 [invert] => 0 [days] => 30) - DateInterval Object ] => 0 [m] => 1 [d] => 0 [h] => 0 [i] => 0 [s] => 0 [反轉] => 0 [天] => 31) .13] –

+0

同上@eskimo,php 5.3.10 –

+0

我認爲我們需要Jon Skeet :) –

回答

0

從PHP文檔:

The DateInterval::format() method does not recalculate carry over points in 
time strings nor in date segments. This is expected because it is not possible 
to overflow values like"32 days" which could be interpreted as anything from 
"1 month and 4 days" to "1 month and 1 day". 

http://php.net/manual/en/dateinterval.format.php

雖然我覺得奇怪的是不同版本的PHP處理呢不同

+0

我認爲這不是原因。我發現時區有影響力。 – Sanne

+0

可能是兩者的結合,很奇怪的行爲!把你在未來遇到同樣問題的人的答案中發現 –