Created
July 30, 2015 09:29
-
-
Save marcojetson/6f4e7e566c2ca0ba97b3 to your computer and use it in GitHub Desktop.
Take a number of seconds and return it in weeks/days/hours/minutes/seconds format. (like mIRC script's $duration identifier)
This file contains 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 | |
/** | |
* @param int $seconds | |
* @return string | |
*/ | |
function duration($seconds) | |
{ | |
foreach (['wk' => 604800, 'day' => 86400, 'hr' => 3600, 'min' => 60, 'sec' => 1] as $symbol => $subunit) { | |
$value = (int) floor($seconds / $subunit); | |
if ($value === 0) { | |
continue; | |
} | |
$parts[] = $value . $symbol . ($value > 1 ? 's' : ''); | |
$seconds -= $value * $subunit; | |
} | |
return isset($parts) ? join(' ', $parts) : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment