time() Format - How many seconds/minutes/hours/days/years/decades ago


/ Published in: PHP
Save to your folder(s)

This snippet will format time() into something a bit more user friendly. Title explains it better.


Copy this code and paste it in your HTML
  1. function _ago($tm,$rcs = 0) {
  2. $cur_tm = time(); $dif = $cur_tm-$tm;
  3. $pds = array('second','minute','hour','day','week','month','year','decade');
  4. $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
  5. for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);
  6.  
  7. $no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]);
  8. if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
  9. return $x;
  10. }

URL: http://www.devwords.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.