Skip to content

Instantly share code, notes, and snippets.

@marcojetson
Created July 30, 2015 09:29
Show Gist options
  • Save marcojetson/6f4e7e566c2ca0ba97b3 to your computer and use it in GitHub Desktop.
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)
<?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