Jump to content

[SOLVED] how to determine if a timestamp is yesterday or today?


dsaba

Recommended Posts

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

<?
$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 :P

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
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.