Seconds to human readable time


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

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.