Code Snippet
Time Ago Function
This can be used for comments and other from of communication to tell the time ago instead of the exact time which might not be correct to some one in another time zone.
The function only uses unix time stamp like the result of time();
Technique #1
<?php
function ago($time)
{
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$difference = $now - $time;
$tense = "ago";
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] 'ago' ";
}
?>Technique #2
function _ago($tm,$rcs = 0) {
$cur_tm = time(); $dif = $cur_tm-$tm;
$pds = array('second','minute','hour','day','week','month','year','decade');
$lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);
$no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]);
if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
return $x;
}Needs a time() value, and it will tell you how many seconds/minutes/hours/days/years/decades ago.
YAY my time ago function is up !!!!
In the first technique, you don’t need the single quotes around ‘ago’ in the return line, unless you were to use
return $difference . $periods[$j] . ' ago';
Or something along those lines
Indeed, the ‘ago’ isn’t only redundant it also breaks the flow when used inline like:
<?php
echo "It was done about " . ago($specialTime) . "). Byebye.";
?>
Instead simply fix it by using $tense as defined
return "$difference $periods[$j] $tense ";
the second function is taken from php.net
http://www.php.net/manual/en/function.time.php#91864
and it should be corrected from
function _ago($tm,$rcs = 0) {
to
function time_ago($tm,$rcs = 0) {
otherwise you’ll get it wrong.
Very useful, thanks alot!
If you make some minor changes, you can specify how many smaller units should be appended to the string (using the 2nd parameter).
Just replace the 7th line with this:
if(($rcs > 0)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm, --$rcs);Example:
time_ago(1291809455)will return “1 month”time_ago(1291809455, 1)will return “1 month 2 weeks” (this works without changing the original)time_ago(1291809455, 2)will return “1 month 2 weeks 4 days”etc.
Used this for my forum latest posts, thanks a lot for this worked perfectly!
Thank you for this snippet! It has served me well in a Facebook Application that I am developing currently. I’ve left you guys a nice comment credit in the source code.
nice :( but mine not working :( please help me :(( :DD thank you very much :DD :DD
demo ? example ?
are we talking about like
It’s been “10 minutes, 28 seconds” since user last posted
or we talking
article “updated 7 minuts ago”?