Test that date is in the last 24 hours?

hi there, I am pulling a particular field from my database which happens to be in mysql datetime format e.g. ‘2010-05-16 03:19:26’

Does anybody know within PHP how i can test that this date is within the last 24 hours? Basically im going to colour the text red if its older than 24 hours and green if its within the last 24 hours.

any help on this would be greatly appreciated

Cheers


if (time() - strtotime("2010-07-04 10:19:26") > 60*60*24) {
   print "Older than 24h";
} else {
   print "Newer than 24h";
}

Erm, probably a MySQL Forums question, but I’ll have a bash! :smiley:


SELECT
     datefield
   , IF((datefield <= NOW()) AND (datefield >= NOW() - INTERVAL 24 HOUR), true, false) AS in_last_24_hours
FROM
     table

Off Topic:

Pawel,

Just a heads up, try to stay away from using plain ol’ maths to create date/time intervals.

It, obviously, doesn’t take into account leap years, daylight saving etc, whereas strtotime… :slight_smile:


<?php
if(strtotime($field) >= strtotime('-1 day')){
  #is
}else{
  #not
}
?>

thanks Guys, just to clarify, it is actually a php question as I will be selecting all records from the DB and using the php code to determine which font colour they are displayed in.

not sure if it can be moved back into the php area

PS - Ive had a little play with the strtotime() function and it does exactly what I wanted, thanks again