Convert Seconds into Time String in PHP


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

This function returns the duration of the given time period in days, hours, minutes and seconds. For example: echo convertSecToStr('654321'); would return "7 days, 13 hours, 45 minutes, 21 seconds"


Copy this code and paste it in your HTML
  1. <?php
  2. function convertSecToStr($secs){
  3. $output = '';
  4. if($secs >= 86400) {
  5. $days = floor($secs/86400);
  6. $secs = $secs%86400;
  7. $output = $days.' day';
  8. if($days != 1) $output .= 's';
  9. if($secs > 0) $output .= ', ';
  10. }
  11. if($secs>=3600){
  12. $hours = floor($secs/3600);
  13. $secs = $secs%3600;
  14. $output .= $hours.' hour';
  15. if($hours != 1) $output .= 's';
  16. if($secs > 0) $output .= ', ';
  17. }
  18. if($secs>=60){
  19. $minutes = floor($secs/60);
  20. $secs = $secs%60;
  21. $output .= $minutes.' minute';
  22. if($minutes != 1) $output .= 's';
  23. if($secs > 0) $output .= ', ';
  24. }
  25. $output .= $secs.' second';
  26. if($secs != 1) $output .= 's';
  27. return $output;
  28. }
  29. ?>

URL: http://www.apphp.com/index.php?snippet=php-convert-seconds-into-time-string

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.