2012-08-23 22 views
0

可能重複:
How to get aggregate days from PHP's DateTime::diff?爲DateInterval ::格式功能不可預期的結果

我有這樣的事情:

$daysDiff = intval($currentDate->diff($dueDate)->format('%R%a')); 

爲DateInterval的PHP文件中: :格式說:

%R -- "+" for positive interval, "-" for negative 

%a -- the total count of days i the interval 

我對結果有麻煩,總是將6015換成天,並帶正確的符號+或 - 。 我用$currentdate$dueDate的不同日期嘗試。 任何人都可以告訴我爲什麼會出現這種行爲。

謝謝

+3

能否請您補充一點,你使用了'$ currentDate'和'$ dueDate'的價值? – andrewsi

+0

$ currentDate = new \ DateTime() $ dueDate是從數據庫中獲取的值,是一個有效的DateTime對象,並且始終(即使使用不同的值)獲得那些6015天的差異。 – Oriam

回答

0

我想你可能會用錯。

From the manual page on DateTime::diff

$datetime1 = new DateTime('2009-10-11'); 
$datetime2 = new DateTime('2009-10-13'); 

$interval = $datetime1->diff($datetime2); 
echo $interval->format('%R%a days'); 

嘗試:

$interval = $currentDate->diff($dueDate); 
echo $interval->format('%R%a'); 

Editted添加....

呵呵。我剛剛運行你的代碼,並且工作。這個問題必須與你是如何產生的DateTime是否:

$datetime1 = new DateTime('2009-10-11'); 
$datetime2 = new DateTime('2009-10-13'); 

$daysDiff = intval($datetime1->diff($datetime2)->format('%R%a')); 
echo $daysDiff 

輸出「2」

+0

請在這裏檢查返回值: $ datetime1-> DIFF($ DATETIME2) 是像一些屬性的對象: y表示一年 m代表蛾 ... 天,總天數,即使在這個天體的價值總是6015 – Oriam

+0

@Oriam - 我不知道你的意思,對不起 – andrewsi

+0

@Oriam - 看我的編輯。我想,你創造的日期有些不妥。 – andrewsi