Posted By

mburica on 10/13/12


Tagged

php time String to


Versions (?)

Seconds to human readable time


 / Published in: PHP
 

Takes a # of seconds and converts it to human readable format. If happened within seconds-years.

  1. $time = time() - $time;
  2.  
  3. $points = array(
  4. 'year' => 31556926,
  5. 'month' => 2629743,
  6. 'week' => 604800,
  7. 'day' => 86400,
  8. 'hour' => 3600,
  9. 'minute' => 60,
  10. 'second' => 1
  11. );
  12.  
  13. foreach($points as $point => $value)
  14. {
  15. if($elapsed = floor($time/$value) > 0)
  16. {
  17. $s = $elapsed>1?'s':'';
  18. $timestamp = "$elapsed $point$s";
  19. break;
  20. }
  21. }

Report this snippet  

You need to login to post a comment.