/ Published in: PHP
a flexible function for making time periods readable
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * A function for making time periods readable * * @author Aidan Lister <[email protected]> * @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 '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) { continue; } if ($count == 0 && !$zeros) { continue; } $seconds = $seconds % $value; } // Build the string foreach ($segments as $key => $value) { $segment = $value . ' ' . $segment_name; if ($value != 1) { $segment .= 's'; } $string[] = $segment; } }