2016-11-24 44 views
-1

我想比較兩個日期,但由於某種原因使用-60天看起來不適合strtotime。strtotime工作不正確-60天

$date = date('Y-m-d H:i:s', strtotime("-60 days")); // gives me string) 2016-09-25 09:27:26 
$today_dt = strtotime($date); 
$expire_dt = strtotime($result['insertedInvoice']); // insertedInvoice shows 2016-11-24 08:56:48 

if($today_dt > $expire_dt){ 
dothis(); 
}else{ 
    dothat(); 
} 

的問題是,在這個功能就應該去爲dothis();,而是它要爲dothat();因爲$ today_dt就像60天的背後,則不能超過$ expire_dt更大。

+1

60天爲一個更小的時間比發票日期。對我來說沒問題。 – apokryfos

+0

你的代碼看起來沒問題。有什麼問題? – RJParikh

+0

根據你的代碼是$ today_dt = 2016-09-25 15:12:00。和$ expire_dt ='2016-11-24 08:56:48'。然後dothat();將會呈現。檢查你的狀況。根據你的評論,條件應該是if($ today_dt <$ expire_dt){ – Elby

回答

1

嘗試以下

$now = date('Y-m-d H:i:s'); 

$date = date('Y-m-d H:i:s', strtotime('-60 days', strtotime($now))); 
$today_dt = strtotime($date); 
$expire_dt = strtotime($result['insertedInvoice']); // insertedInvoice shows 2016-11-24 08:56:48 

if($today_dt > $expire_dt){ 
dothis(); 
}else{ 
    dothat(); 
} 

**我寫這篇文章的代碼按你的代碼,請檢查如果條件。如果需要改變如果條件如下 (我有點困惑你的最後一句話)

if($today_dt < $expire_dt){ 
    dothis(); 
} else { 
    dothat(); 
} 
1

你可以使用日期時間改爲:

$date = new DateTime('-60 days'); 
echo $date->format('Y-m-d H:i:s'); 
0
$today = date('Y-m-d H:i:s'); //Today 
echo $date = date('Y-m-d H:i:s', strtotime($today . ' - 60 days')); 

Hope this will helps you.