charlie
charlie

Reputation: 1384

PHP If statement displaying incorrect results

I am running this PHP Code:

$earliest='2013-11-19 23:00:00';
$latest='2013-11-19 01:00:00';

if(isset($earliest) and isset($latest))
{
    if(date("Y-m-d H:i:s") >= $earliest and date("Y-m-d H:i:s") <= $latest)
    {
        echo 'yes';
    }
    else
    {
        echo 'no';
    }
}

the current date/time (2013-11-19 12:52:00) is > or = to the $earliest variable but its displaying no

any ideas why this would be? have i done something wrong in my code?

if i do just:

if(date("Y-m-d H:i:s") <= $latest)

it displays yes

Upvotes: 1

Views: 60

Answers (2)

hammus
hammus

Reputation: 2612

Your comparison operators are incorrect:

 if(date("Y-m-d H:i:s") >= $earliest and date("Y-m-d H:i:s") <= $latest)

should be:

 if(date("Y-m-d H:i:s") >= $earliest && date("Y-m-d H:i:s") <= $latest)

Upvotes: 0

minboost
minboost

Reputation: 2563

Your earliest is actually after your latest (11pm vs 1am)

Upvotes: 2

Related Questions