39

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?

4
  • 1
    do you mean "on this day" or within the last 24 hours? Commented Apr 25, 2011 at 4:11
  • 3
    @Mark This might help en.wiktionary.org/wiki/today ;) Commented Apr 25, 2011 at 4:28
  • 6
    definition of today: "this day" Commented Apr 25, 2011 at 4:53
  • @Mark so when someone expects you to go to the shop today, they might have a surprise... Commented Jul 5, 2022 at 15:48

7 Answers 7

115

I think:

date('Ymd') == date('Ymd', strtotime($timestamp)) 
Sign up to request clarification or add additional context in comments.

11 Comments

i think check equal between 2 integer is better than 2 string
@Davood I'd be curious to know why.
1. if case sensitive of 2 string be diffrent, return false, 2. for php optimization check equal between 2 integer is more optimal between 2 string . this is my idea .
Technically, they're both strings. They will be evaluated as two integers though (PHP is crazy). In thise case that doesn't matter, because both sides are 8 characters (digits) long and none start with 0. If either were false, you'd have to use ===.
@Davood You're explicitly creating two strings of the format 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.
|
9
if (date('Y-m-d') == date('Y-m-d', strtotime('2011-02-12 14:44:00'))) { // is today } 

Comments

1
$offset = date('Z'); //timezone offset in seconds if (floor(($UNIX_TIMESTAMP + $offset) / 86400) == floor((mktime(0,0,0) + $offset) / 86400)){ echo "today"; } 

Comments

1

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 } 

Comments

0

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

1 Comment

Some explanation about your answer would be helpful
0
(date('Ymd') == gmdate('Ymd', $db['time']) ? 'today' : '') 

Comments

-1

are you mean this ?

if( strtotime( date( 'Y-m-d' , strtotime( '2011-02-12 14:44:00' ) ) ) == strtotime( date( 'Y-m-d' ) ) ) { //IS TODAY; } 

3 Comments

why ? i think it's more better !
It's more better..? Sure... but it's still a waste of date calculations. Without the 2 strtotime's, you know enough to make the evaluation.
Furthermore, this may result in incorrect values. UNIX timestamps are seconds and 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.