A few days ago I was looking for a php function that showed the date/time format like on the site Digg (ex: 15 min 20 sec ago). I found two different php functions on the internet that almost achieved what I was looking for, at these links:
woork.blogspot.com/2007/10/time-and-date-difference-using-php.html
drupal.org/node/61565
But it wasn't exactly what I wanted so I wrote my own php function for it, check it out below.
woork.blogspot.com/2007/10/time-and-date-difference-using-php.html
drupal.org/node/61565
But it wasn't exactly what I wanted so I wrote my own php function for it, check it out below.
function timeAgo($timestamp, $granularity=2, $format='Y-m-d H:i:s'){
$difference = time() - $timestamp;
if($difference < 0) return '0 seconds ago'; // if difference is lower than zero check server offset
elseif($difference < 864000){ // if difference is over 10 days show normal time form
$periods = array('week' => 604800,'day' => 86400,'hr' => 3600,'min' => 60,'sec' => 1);
$output = '';
foreach($periods as $key => $value){
if($difference >= $value){
$time = round($difference / $value);
$difference %= $value;
$output .= ($output ? ' ' : '').$time.' ';
$output .= (($time > 1 && $key == 'day') ? $key.'s' : $key);
$granularity--;
}
if($granularity == 0) break;
}
return ($output ? $output : '0 seconds').' ago';
}
else return date($format, $timestamp);
}
$difference = time() - $timestamp;
if($difference < 0) return '0 seconds ago'; // if difference is lower than zero check server offset
elseif($difference < 864000){ // if difference is over 10 days show normal time form
$periods = array('week' => 604800,'day' => 86400,'hr' => 3600,'min' => 60,'sec' => 1);
$output = '';
foreach($periods as $key => $value){
if($difference >= $value){
$time = round($difference / $value);
$difference %= $value;
$output .= ($output ? ' ' : '').$time.' ';
$output .= (($time > 1 && $key == 'day') ? $key.'s' : $key);
$granularity--;
}
if($granularity == 0) break;
}
return ($output ? $output : '0 seconds').' ago';
}
else return date($format, $timestamp);
}


I am Wesley Luyten, on the net I'm also known as wessite. I am born and raised in Belgium, but I also have Canadian blood running through my body. I'm currently learning for my degree in engineer ICT, and I'm pretty much web addicted. I use a macbook pro to handle my computer business.