Comparing Dates with PHP

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Can't understand why this doesn't work and is driving me insane :(

I'm trying to highlight a table row a different color if a date is today's date, in the the past and has not yet occurred.

E.g.

PHP:
$theDate = date('D M jS',strtotime($row[''theDate]));

$today = date('D M jS');
$today = date('D M jS', strtotime($today));

If I echo out the today variable I get Wed Aug 19th and echoing the variable called theDate I get Wed Aug 19th.

This is the bit that annoys me. Highlighting the current day works as expected, but highlighting past and future dates doesn't (everything other than today is highlighted green):

PHP:
if ($today == $theDate) {
         $highlight_today = "style=\"background:#FFBABA;\"";
} elseif ($theDate > $today) {
         $highlight_today = "style=\"background:#DFF2BF;\"";
} else {
         $highlight_today = "style=\"background:#fff;\"";
}


Can anyone shed some light on this, as I'm sure I'm missing something obvious.

Thanks
 
why not compare them in linux timestamp?
eg;
PHP:
$theDateStamp = strtotime($row['theDate']);
$today = mktime(0,0,0);
if ($today == $theDateStamp){
 // do stuff
}
 
Last edited:
PHP:
$theDateStamp = strtotime($row[''theDate]);
spot the mistake?

anywho..
PHP:
if (time() > strtotime($row['theDate'])) {
  // theDate is in the past
} else {
  // theDate is in the future
}
 
Last edited:
Back
Top Bottom