Created
April 25, 2011 20:12
-
-
Save danielgwood/941117 to your computer and use it in GitHub Desktop.
Number of seconds -> neatly formatted duration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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