Jump to content

Changing a UNIX timestamp into "Today" and "Yesterday"


CMellor

Recommended Posts

Odd as this may sound, I have only just discovered the [b]strtotime()[/b] function. I like the looks of what it is capable of and have tried to use it to my advantage. I tried to make a little script that would convert a UNIX time stamp into a message saying whether or not the stamp was a time for today, or yesterday, or if neither, then just display the date it is.

I'm not a PHP pro, but am always learning. I have looked all over for tutorials on how to do this, but the only useful one's I found are for blogging software. I was hoping someone here could guide me in the right direction of where to begin coding this script, or maybe provide me with a sample that I can toy around with myself. Any help at all would be appreciated.

Thanks for reading,

Chris.
Well, once you've got a UNIX timestamp, you can use the date() function to parse out the year, month and day of the timestamp. Just check to see whether this date is equivalent to yesterday or today and execute accordingly:
[code]
<?php
if (date('Y-m-d', $timestamp) == date('Y-m-d')) {
  // The timestamp is from today!
} elseif (date('Y-m-d', $timestampe) == date('Y-m-d', strtotime("Yesterday"))) {
  // The timestamp is from yesterday!
} else {
  // The timestamp is from neither today nor yesterday!
}
?>
[/code]

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.