2012-11-08 95 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;