duration()

By Conscious on Apr 26, 2011

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"));
}
?>

Comments

Sign in to comment.
Conscious   -  May 01, 2011

Yes, thanks Plornt.
With the seconds, it is currently ($s%604800%86400%3600%60)
Would it just work with ($s%60)?

 Respond  
[Plornt]   -  Apr 28, 2011

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?

 Respond  
Conscious   -  Apr 28, 2011

Edited it to add in the second parameter (optional), however the second parameter will only be helpful if the number isn't an even number, as duration('5','hours) will return 5 hours. (duh)

Serpent would you mind explaining how your contribution works?

 Respond  
Conscious   -  Apr 28, 2011

Oh. I understood Hawkee to suggestion as to be able to select the return unit of measure.
Re-reading Hawkee's comment, your interpretation makes more sense now >.>

 Respond  
Serpentsounds   -  Apr 28, 2011

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.

 Respond  
Conscious   -  Apr 28, 2011

or what it does in fact

 Respond  
Conscious   -  Apr 28, 2011

I'm not sure quite how to use that in the script Serpentsounds..

 Respond  
Serpentsounds   -  Apr 28, 2011

$scale = array('minutes' => 60, 'hours' => 3600, 'days' => 86400, 'weeks' => 604800);
$s *= (isset($measure) && isset($scale[$measure])) ? $scale[$measure] : 1;

 Respond  
Conscious   -  Apr 28, 2011

Do you mean also accepted days? Hours is supported in this.
I'm planning on making it go up into weeks, as that's the highest constant.

How would the unit of measure parameter work?

 Respond  
Hawkee   -  Apr 27, 2011

It would be nice if it had a second parameter indicating the unit of measure. I'd find this more useful if it also accepted hours.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.