Posted By

kendsnyder on 01/18/11


Tagged

String convert


Versions (?)

Convert media duration to hours, minutes, seconds


 / Published in: PHP
 

  1. function secondsToDuration($s) {
  2. $time = round($s);
  3. $parts = array();
  4. while ($time >= 1) {
  5. array_unshift($parts, $time % 60);
  6. $time /= 60;
  7. }
  8. if ($s < 60) {
  9. // if it is seconds only, prepend "0:"
  10. array_unshift($parts, '0');
  11. }
  12. $last = count($parts) - 1;
  13. if ($parts[$last] < 10) {
  14. $parts[$last] .= '0';
  15. }
  16. $duration = join(':', $parts);
  17. return $duration;
  18. }

Report this snippet  

You need to login to post a comment.