Making time periods readable
Posted on April 5th, 2004 in Code Repository | 43 Comments »
This is a flexible function for making time periods readable.
It allows for the conversion of an integer number of seconds into a readable string. For example, “121″ into “2 minutes, 1 second”.
The $use parameter allows you to define which time periods should be used (default is null, which is all time periods). The $zeros parameter allows you to set whether zero time periods should be displayed, e.g. “0 years, 0 months” (defaults to false).
/**
* A function for making time periods readable
*
* @author Aidan Lister <aidan@php.net>
* @version 2.0.1
* @link http://aidanlister.com/2004/04/making-time-periods-readable/
* @param int number of seconds elapsed
* @param string which time periods to display
* @param bool whether to show zero time periods
*/
function time_duration($seconds, $use = null, $zeros = false)
{
// Define time periods
$periods = array (
'years' => 31556926,
'Months' => 2629743,
'weeks' => 604800,
'days' => 86400,
'hours' => 3600,
'minutes' => 60,
'seconds' => 1
);
// Break into periods
$seconds = (float) $seconds;
$segments = array();
foreach ($periods as $period => $value) {
if ($use && strpos($use, $period[0]) === false) {
continue;
}
$count = floor($seconds / $value);
if ($count == 0 && !$zeros) {
continue;
}
$segments[strtolower($period)] = $count;
$seconds = $seconds % $value;
}
// Build the string
$string = array();
foreach ($segments as $key => $value) {
$segment_name = substr($key, 0, -1);
$segment = $value . ' ' . $segment_name;
if ($value != 1) {
$segment .= 's';
}
$string[] = $segment;
}
return implode(', ', $string);
}
Let’s have a look at some examples:
echo time_duration(100000000); echo time_duration(100000000, null, true); echo time_duration(100000000, 'yMw'); echo time_duration(100000000, 'd');
This would produce the following output:
3 years, 2 months, 19 hours, 22 minutes, 16 seconds 3 years, 2 months, 0 weeks, 0 days, 19 hours, 22 minutes, 16 seconds 3 years, 2 months 1157 days
43 Responses
oh!!! its greats! you are a genius!
=)
Nice work
big tx !
Nice script!
Thanks much! This is exactly what I was looking for.
Nice work! Big thanks!
Wow, it’s awsome !
Thanks for sharing
Very good. Thanks!
Cool. Thx for da script yo!
Great hack. Thx for sharing. Solved my problem. ( From Brazil )
Holy crap, this is useful! I used this in the “manage my files” section of hostfile.org. Eventually I’ll get around to creating a “credits” page and I’ll definately credit you at this site. Great work! This should really be a function in PHP, for sure…
Very useful stuff. I happen to get directed here from the PHP manual a lot.
Keep up the good work!
(from germany) WOW!! this reallyreallyreally should be a php-function! thank you for this wonderfull piece of code!
Dear Sir,
Excellent class to deal with dates. Something that is used most and is always tiring to code as lot of thinking is involved. Your code plus add ons are excellent.
PHP rocks !
Thanks !
Chantu of ChantuDotCom
Very nice class function for date manipulation. Keep up the good work and hope to see more!!!!!!
As with everyone I added my own alterations, (I set a “standard” period that is only months, weeks, and days, as well as setting it to only add to the array (in array2string) if $value > 0.
(I also added the static keyword for PHP5 OOP compatibility.)
Thanks so much for this. I would’ve spent a couple hours re-doing this script had I not seen yours. Keep up the good work!
Thanks! We’re using this in our advertising platform SDK.
Very usefull stuff. it has helped me a ton.
Keep going even i wish to contribute in such kind of work..
Thx a lot!!!! This function very help me!!!!
[...] ned dagene til jul, på en pen menneskevennlig måte, i PHP. Koden i denne posten er hentet fra Aidan Lister. Det eneste jeg har gjort er å oversette months til måneder, [...]
I have search for about 4 hour on the web, and I not found nothing like this.
P E R F E C T!!!
So, big thanks for share it.
So how did you arrive at your figures for months and years?
(I realise how you did it but it seems silly to use these average figures when there can be no definitive answer without a referenece point in time ffrom which to measure!)
Perhaps it would be better to indicate that the figures for months and years are approximate?
…or leave out the years and months parts altogether and only use those periods which are an exact time (eg. days, hours, minutes and seconds)…
Hi Simon,
The month and year values are the number of seconds in a solar month and year respectively. In response to your second question, you can easily achieve that with: time_duration(100000000, ‘wdhms’); … personally I don’t care if an event “2 months, 3 days” in the future differs from the calendar interpretation by a day or two.
Thanks – very very useful. I agree that some time duration function should be a function in PHP.
Thank you, really helpfull
Thanks! GPL
Nice piece of work
really helpful i like it.
This is great! Exactly what I was looking for. I even was able to modify the days value to 7 hours (work time of the day) to adapt it to my use. However I can’t get the $zero = true to work. It still shows 0 days…
And last, how can I get the names to be French? Example: “days: display as “jours”. Thanks!
This is a great function. The best one i’ve seen after days of googling the web. Just be aware that the function will break if the time duration is zero. To work for 0 time duration just add:
if (empty($segments)){
$segments['seconds'] = 0;
}
before you build the string
GooD done!
totally fucked, years 366 / 365 months 28/29/30/31
toto, both years and months use the astronomical mean. Do you really think your visitors care if you’re half a day off on a date that’s a year away?
Thanks! Very useful.
Just wanted to point out a few bugs I found while working through this script.
I guess you cant post PHP within PHP tags…
echo time_duration(123, ‘yMd’);
echo time_duration(0);
thanks for information!
Fantastic!!
What would I have to do to add tenths and hundredths of a second?
Trying to nail down sub-second durations.
Thanks a bunch!
Mark
thnx…..alot…….for sharing……
Thank you, great function.
Thanks for sharing!
Modified to return compact strings: http://codepad.org/w1GrPTjY
duration(234567,true) = "2d 17h 9m 27s"