I have gotten many requests for this snippet which i use here on Netnaija.com to show time of actions as either x seconds ago, x minutes ago, x hours ago, x days ago, x months ago or x years ago. Its of course in PHP. So incase you need it here it is.
You should supply a UNIX Timestamp as $ptime. To use, you simply call $timeAsAgo = timeAgo($timeStamp);.
PHP Code
<?php
function timeAgo($ptime)
{
$etime = time() - $ptime;
if ($etime < 1)
{
return '0 seconds';
}
$a = array( 12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($a as $secs => $str)
{
$d = $etime / $secs;
if ($d >= 1)
{
$r = round($d);
return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
}
}
}
?>
Comments