A version of mIRC's $duration for PHP.
Usage: duration(seconds)
Eg:
echo duration(55);
returns: 55 seconds
Eg:
echo duration(3802);
Returns: 1 hour 3 minutes 22 seconds
There is also, in adition, a second parameter;
duration(time,minutes/hours/days/weeks)
changes what unit of measurement the time is in.
Only useful if the time is a decimal otherwise duration(5,hours) is '5 hours'.
<?php
function duration($s,$measure=""){
$scale = array('minutes' => 60, 'hours' => 3600, 'days' => 86400, 'weeks' => 604800);
if (!empty($scale[$measure])) $s *= $scale[$measure];
return preg_replace('/((^|\s+)1 (.+?))s/i','\1', preg_replace('/(^|\s+)0 (\S+)/i', '',floor(($s/604800))." weeks ".floor((($s%604800)/86400))." days ".floor((($s%604800%86400)/3600))." hours ".floor((($s%604800%86400%3600)/60))." minutes ".($s%604800%86400%3600%60)." seconds"));
}
?>
Its an inline if that checks if $measure is set AND its inside of the array. It then timeses $s by the number inside the array.
Its the same as:
$scale = array('minutes' => 60, 'hours' => 3600, 'days' => 86400, 'weeks' => 604800);
if (!empty($scale[$measure])) $s *= $scale[$measure];
if slightly expanding the if makes any more sense to you?
Hawkee's suggestion to specify a unit of measure meant that it would be convenient if the user didn't always need to give the duration in seconds. With an option parameter $measure, you could call duration(3, 'hours'); and that bit from my previous posted would translate that into 10800 before performing the regex substitution.