dsaba Posted January 11, 2008 Share Posted January 11, 2008 What is logic for determining if a given timestamp is today or yesterday? I know that there are 86400 seconds in 1 day, but this determining if the timestamp is less than 1 day or more than 1 day older than the current timestamp is not the problem. For example, 1:01 AM versus 11:59 PM, both timestamps have less than 86400 seconds between them, but one is in fact the day before the other. I tried this: //strtotime ( string $time [, int $now ] ) $now = time(); $yesterday = strtotime("-1day", $now); $diff = $yesterday - $giveTimestamp; if ($diff < 86400) { //today } elseif ($diff > 86400 && $diff < 86400*2) { //yesterday } else { //not today or yesterday } -thanks for looking Link to comment https://forums.phpfreaks.com/topic/85489-solved-how-to-determine-if-a-timestamp-is-yesterday-or-today/ Share on other sites More sharing options...
Nhoj Posted January 11, 2008 Share Posted January 11, 2008 <? $timestamp = SOMETIMESTAMP; $currentTime = time(); $d = date('d', $time); $m = date('m', $time); $y = date('Y', $time); $midnight = mktime(0, 0, 0, $m, $d, $y); // Gets the timestamp for 12 AM of the current day. if ($timestamp < $midnight) { // The timestamp is older than midnight of today } else { // The timestamp is after midnight of today } ?> Replace SOMETIMESTAMP with the timestamp you're checking to see if it is from yesterday or today Edit: Had the comments backwards Link to comment https://forums.phpfreaks.com/topic/85489-solved-how-to-determine-if-a-timestamp-is-yesterday-or-today/#findComment-436281 Share on other sites More sharing options...
dsaba Posted January 11, 2008 Author Share Posted January 11, 2008 Thanks for the input, here's my solution: //$givenTimestamp is timestamp in question $midnightYes = strtotime("midnight yesterday"); $midnightToday = strtotime("midnight today"); if ($givenTimestamp > $midnightToday) { //today } elseif ($givenTimestamp > $midnightYes && $givenTimestamp < $midnightToday) { //yesterday } else { //older than yesterday } Link to comment https://forums.phpfreaks.com/topic/85489-solved-how-to-determine-if-a-timestamp-is-yesterday-or-today/#findComment-436287 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.