2012-11-08 93 views
0

if語句PHP if語句比較我有麻煩寫3個日期

有3個變量

$date = 1985-11-01; 

$date2 = 2005-11-08; 

$date3 = 2006-11-08; 

,這裏是我的if語句。

if($date > $date2 && $date < $date3) { 
      // dob is between the limits 
      return TRUE; 
     } 
     else { 
      // dob is outside the limits 
      return FALSE; 
     } 

我試着做的是,如果$ date不在$ date2和$ date3之間,則返回false。我今天很累,我的大腦不工作,有人能告訴我我做錯了什麼嗎?

+0

究竟是行不通的,如果你的日期已經被存儲爲UNIX時間戳? – lanzz

回答

2

您可以使用strtotime,以確保您比較正確。

$date = strtotime('1985-11-01'); //499680000 

$date2 = strtotime('2005-11-08'); //1131436800 

$date3 = strtotime('2006-11-08'); //1162972800 

當你做你的邏輯,看看Unix的產生時間戳...

+0

啊,你編輯雙關語! – Nanne

+1

@Nanne它在裏面燃燒着我。 – Kermit

+0

PHP DateTime對象可能更直觀,更靈活(可以處理前期日期)來使用。 – Sanchises

0

轉換您的變量時對象使用strtotime

$my_date = strtotime('08/11/2012');

+0

是的,我的變量是這樣的,我只是覺得我會節省時間,並把日期 – user979331

+0

就像已經?你使用了'strtotime()'函數嗎? –

0
//using strtotime convert your date(s) in time stamp then your checking will be correctly worked. 

$date = strtotime('1985-11-01'); 

$date2 = strtotime('2005-11-08'); 

$date3 = strtotime('2006-11-08'); 

if($date > $date2 && $date < $date3) 
     return true; 
else 
     return false;