Twitter Style “Time Ago” Dates on PHP

0
664

The following function will show the time like Twitter with that info ago, like 9 hours ago:

function _agotime($tm,$rcs = 0) {
   $cur_tm = time(); $dif = $cur_tm-$tm;
   $label = array('second','minute','hour','day','week','month','year','decade');
   $lengh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
   for($v = sizeof($lengh)-1; ($v >= 0)&&(($no = $dif/$lengh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lengh[$v]);

   $no = floor($no); if($no <> 1) $label[$v] .='s'; $x=sprintf("%d %s ",$no,$label[$v]);
   if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
   return $x;
}

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here