I've got a timestamp in the following format (Which can easily be changed thanks to the beauties of PHP!).
2011-02-12 14:44:00
What is the quickest/simplest way to check if this timestamp was taken today?
I've got a timestamp in the following format (Which can easily be changed thanks to the beauties of PHP!).
2011-02-12 14:44:00
What is the quickest/simplest way to check if this timestamp was taken today?
I think:
date('Ymd') == date('Ymd', strtotime($timestamp)) ===.Y-m-d there, no case sensitivity problems possible. Maybe two integers are faster to compare, but you'll have to convert both into an integer before you can compare them, which is more overhead than comparing two strings directly.This is what i use for this kind of task :
/** date comparator restricted by $format. @param {int/string/Datetime} $timeA @param {int/string/Datetime} $timeB @param {string} $format @returns : 0 if same. 1 if $timeA before $timeB. -1 if after */ function compareDates($timeA,$timeB,$format){ $dateA=$timeA instanceof Datetime?$timeA:(is_numeric($timeA)?(new \Datetime())->setTimestamp($timeA):(new \Datetime("".$timeA))); $dateB=$timeB instanceof Datetime?$timeB:(is_numeric($timeB)?(new \Datetime())->setTimestamp($timeB):(new \Datetime("".$timeB))); return $dateA->format($format)==$dateB->format($format)?0:($dateA->getTimestamp()<$dateB->getTimestamp()?1:-1); } compare day : $format='Y-m-d'.
compare month : $format='Y-m'.
etc...
in your case :
if(compareDates("now",'2011-02-12 14:44:00','Y-m-d')===0){ // do stuff } I prefer to compare timestamps (rather then date strings), so I use this to check today.
$dayString = "2011-02-12 14:44:00"; $dayStringSub = substr($dayString, 0, 10); $isToday = ( strtotime('now') >= strtotime($dayStringSub . " 00:00") && strtotime('now') < strtotime($dayStringSub . " 23:59") ); Fiddle: http://ideone.com/55JBku
are you mean this ?
if( strtotime( date( 'Y-m-d' , strtotime( '2011-02-12 14:44:00' ) ) ) == strtotime( date( 'Y-m-d' ) ) ) { //IS TODAY; } strtotime's, you know enough to make the evaluation.strtotime fills in any information that's not in the given date string from the current time. I.e. the hours, minutes and seconds part of strtotime('2011-02-12') will be taken from the current time. In the rare circumstance that the time advances by one second between the first call to strtotime and the second, you may get two different timestamps for the same day.