Skip to content

Instantly share code, notes, and snippets.

@danielgwood
Created April 25, 2011 20:12
Show Gist options
  • Save danielgwood/941117 to your computer and use it in GitHub Desktop.
Save danielgwood/941117 to your computer and use it in GitHub Desktop.
Number of seconds -> neatly formatted duration
<?php
/**
* Parse a number of seconds into a more readable format.
*
* @param int $seconds Length of time.
* @param string $minSeparator Placed between min and second values.
* @param string $hourSeparator Placed between hour and minute values.
* @return string
*/
public static function duration($seconds, $minSeparator = ':', $hourSeparator = ':')
{
// Work out chunks
$seconds = (int)$seconds;
$hours = intval($seconds / 3600);
$minutes = intval(($seconds / 60) % 60);
$seconds = str_pad(intval($seconds % 60), 2, '0', STR_PAD_LEFT);
// Combine with separator
$duration = $minutes . $minSeparator . $seconds;
if($hours != 0) {
$duration = $hours . $hourSeparator . $duration;
}
return $duration;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment