user979331
user979331

Reputation: 11851

PHP if statement compare 3 dates

I am having trouble writing if statement

there are the 3 variables

$date = 1985-11-01;

$date2 = 2005-11-08;

$date3 = 2006-11-08;

and here is my if statement.

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

What I am try to do is, if $date is not in between $date2 and $date3, return false. I am very tired today and my brain is not working, can someone tell me what I am doing wrong?

Upvotes: 1

Views: 3659

Answers (3)

Tapas Pal
Tapas Pal

Reputation: 7207

//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;

Upvotes: 0

Kermit
Kermit

Reputation: 34055

You can use strtotime to ensure you compare correctly.

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

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

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

When you do your logic, look at the Unix generated timestamps...

Upvotes: 3

Pedro del Sol
Pedro del Sol

Reputation: 2841

convert your variables to time objects using strtotime

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

Upvotes: 0

Related Questions