Facebook-like time ago with PHP

November 11th, 2011 by Alex Leave a reply »

Ever wanted to have your date display like Facebook has? “3 minutes ago” instead of “09:12:10”?

Well, here’s a very simple code with 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;
   if ($difference > 0)
      $tense = 'ago';
   else
      $tense = 'in future';
 
   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] $tense ";
}
 
echo "I was here ".ago(time()-3600);
// will output "I was here 1 hour ago"

Another way is to use a function from andypsv

function time_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;
}

Hope you found it handy, I did.


Comments are closed.