Todays Date > the $todate = says yes, but it isn't.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Todays Date > the $todate = says yes, but it isn't.

Post by simonmlewis »

Code: Select all

$todate = new DateTime($row->dateto);
if ($today > $todate)
{ echo "<div style='border: 1px dotted #ff0000; padding: 2px; font-size: x=small;'>EXPIRED</div>";}
The $row->dateto here is 17th September 2010.
Today's date is 17th September 2010.

This is showing the Expired result.
And when i do:

Code: Select all

$calc = ($todate < $today);
echo "$calc";
It gives a result of 1.
It should be zero, or empty shouldn't it?

I'm trying to show EXPIRED when an entry is either past the TO DATE.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Todays Date > the $todate = says yes, but it isn't.

Post by Eran »

DateTime contains the time as well as the date. If you pass in only the date, the time will be set as the beginning of the date (00:00:00) - so this comparison will almost always fail for the current date.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Todays Date > the $todate = says yes, but it isn't.

Post by simonmlewis »

Code: Select all

$today = (date('Y-m-d'));
$todaydate = strtotime($today);
$fromdate = strtotime($row->datefrom);
$todate = strtotime($row->dateto);
if ($todaydate > $todate)
Bingo - thanks a lot.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply