Function For Relative Timestring
Posted March 15th 2012, 5:34pm
When storing the time of an event such as a post or a comment, the time is extremely often stored in Unix Format.
This makes it a lot easier to compare two events.

This thread will provide you with two functions which will make a nice string of text from a Unix timestamp which will be relative to the event, by this I mean:
- A few seconds ago, a few minutes ago, about an hour ago ... etc.

It is split up into 2 functions so when outputting a date or time from a unix timestamp, you can choose which one will better suit your needs at that place.

I'll comment the codes mildly throughout, to help you understand whats going on.

/*
* Relation time string functions for a unix time stamp.
* Copyright by LGforum @ Avacweb.com, use and/or modification is allowed provided
* this entire copyright notice remains. No distribution without authors consent.
*/
function day_time_string($unix)
{
$event=getdate($unix); //array containing date information of the event.
$now=getdate(time()); //array with date information of right now.

//a few minor fixes for displaying times correctly.
$hour=$event['hours'];
$am_pm='AM';
if($hour > 12) { $hour=$hour-12; $am_pm="PM"; }
$min = $event['minutes'];
if(strlen($min) == 1){ $min = '0' . $min; }

if($event['year'] == $now['year']) //if the event year is the same as this year
{
if($event['mon'] == $now['mon']) //if event is in the same month.
{
if($event['mday'] == $now['mday']) //if event is in the same day.
{
return 'Today at '.$hour.':'.$min.$am_pm; //format: Today at 00:00AM
}
else if($event['mday'] == $now['mday']-1 ) //one day ago.
{
return "Yesterday at ".$hour.":".$min.$am_pm; //format: Yesterday at 00:00AM
}
else //this same week, but not same day, and not yesterday.
{
return "On " .$event['weekday']." at ".$hour.":".$min.$am_pm; //format: On Tuesday at 8:45PM
}
}
else //not this month. but less than a year ago.
{
return "At ".$hour.":".$min.$am_pm.' '.$event['weekday'] .", ". $event['mday'] ." ".$event['month'];
//format: At 4:53PM Monday 25 June
}
}
else //not in the same year.
{
return $hour.":".$min.$am_pm.' '.$event['weekday'] .", ". $event['mday'] ." ".$event['month'].' '.$event['year'];
//format: 4:50PM Monday 20 September 2006
}
}

function relative_timestring($unix)
{
$diff=time()-$unix; //difference between now and the event, in seconds.
$aday=3600*24; //value = to one day in seconds.

if($diff<$aday) //less than 24 hours ago.
{
if($diff<3600) //less than an hour ago.
{
if($diff<60) //less than a minute ago.
{
if($diff<20) //less than 20 seconds ago.
{
return "A few seconds ago";
}
return "Less than a minute ago";
}

elseif($diff<(60*5)) //less than 5 mins ago.
{
return "A few minutes ago";
}

return floor( $diff/60 )." minutes ago";
}
elseif($diff<(3600*5)) //less than 5 hours ago. but more than an hour ago.
{
return "A few hours ago";
}
return floor( $diff/3600 )." hours ago";
}
return day_time_string($unix);
}


All that is required is to send in the unix timestamp. Eg.
echo 'Comment posted ' . relative_timestring($comment['timestamp']);


As you can see the, second function creates it in the form of 'A few seconds ago' or 'A few minutes ago' etc. If the event occured more than five minutes ago, it will pass the unix timestamp into the first function and return the time in the format the first function provides.

Hope this helps you in any future projects! :)
φ
Posts: 509
Joined: July 9th 2011, 11:00pm
Location: England
Likes Given: 15
Likes Received: 26

Who is online

Users browsing this forum: No registered users and 1 guest