Posted By

Unreal on 07/22/09


Tagged

time ago


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

kirik
irishsk
dantreacy


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


 / Published in: PHP
 

URL: http://www.devwords.com

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

  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. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: leei on April 28, 2011

Hi

You need to login to post a comment.