Making time periods readable
Posted on April 5th, 2004 in Code Repository | 19 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.0
* @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;
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
foreach ($segments as $key => $value) {
$segment_name = substr($key, 0, -1);
$segment = $value . ' ' . $segment_name;
if ($value != 1) {
$segment .= 's';
}
$array[] = $segment;
}
$str = implode(', ', $array);
return $str;
}
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
19 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..