Ago date or time - Time ago/Day ago
"year+month+day+time ago" PHP function
<?php
function time_ago($date,$granularity=2) {
$date = strtotime($date);
$difference = time() - $date;
$periods = array('decade' => 315360000,
'year' => 31536000,
'month' => 2628000,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1);
if ($difference < 5) { // less than 5 seconds ago, let's say "just now"
$retval = "posted just now";
return $retval;
} else {
foreach ($periods as $key => $value) {
if ($difference >= $value) {
$time = floor($difference/$value);
$difference %= $value;
$retval .= ($retval ? ' ' : '').$time.' ';
$retval .= (($time > 1) ? $key.'s' : $key);
$granularity--;
}
if ($granularity == '0') { break; }
}
return ' posted '.$retval.' ago';
}
}
//call a function here
echo $date = time_ago('2011-04-22 16:59:06');?>
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.