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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.