2012-02-17 40 views
0
function is_due($due_date) 
     { 
      $now=new DateTime('now'); 
      $dnow=$now->format('Y-m-d'); 
      $due=$due_date->format('Y-m-d'); 
      $interval =(strtotime($dnow)-strtotime($due)); 
     print_r($due_date->format('Y-m-d')); 
     return $interval; 
     } 

我使用該功能,以檢查是否給定的日期是由於檢查日期時間失敗?

的$ DUE_DATE從外部文件被分配作爲數組的字符串「2012年12月12日」被檢索後經過在元素,例如,

$ datetime ['due'] = ReadExtFile(); 當我調用該函數爲print_r(is_due($datetime['due']));

$due=new DateTime($datetime['due']); 

似乎沒有任何工作,我看不到輸出。但print_r($datetime)將顯示[due]的結果。

+0

如果你做print_r(is_due($ datetime)),會發生什麼? – MysticXG 2012-02-17 04:26:15

+0

嗯。爲什麼是時間? '$ interval = $ now-> getTimeStamp() - $ due_date-> getTimeStamp()'。 – 2012-02-17 04:38:43

+0

謝謝你現在的作品。 – Mikhail 2012-02-17 04:41:31

回答

0

這種方法寫入的方式,它期望$ due_date是一個DateTime對象,而不是一個字符串。您表示您傳遞的是字符串而不是對象。

看起來你只是想返回$ interval作爲從給定的日期到現在的計數。我建議只使用一個只處理字符串的簡單方法。

function is_due($due_date) { 
    return time() - strtotime($due_date); 
    // return ((time() - strtotime($due_date)) >= 0); // depending on function you want this might work 
}