function time_ago($timestamp, $granularity = 2) {
$timestamp = time() - $timestamp;
$units = array('1 year|%d years' => 31536000,
'1 week|%d weeks' => 604800,
'1 day|%d days' => 86400,
'1 hour|%d hours' => 3600,
'1 min|%d mins' => 60,
'1 sec|%d secs' => 1
);
$output = '';
foreach ($units as $key => $value) {
$key = explode('|', $key);
if ($timestamp >= $value) {
$pluralized = floor($timestamp / $value) > 1 ?
sprintf($key[1], floor($timestamp / $value)) :
$key[0];
$output .= ($output ? ' ' : '') . $pluralized;
$timestamp %= $value;
$granularity--;
}
if ($granularity == 0) {
break;
}
}
return $output ? $output : "Just now";
}
It should be close.
Edit: the following line has been added: $ timestamp = time () - $ timestamp;
source
share