-
Notifications
You must be signed in to change notification settings - Fork 8
/
time_duration.php
52 lines (49 loc) · 1.39 KB
/
time_duration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* A function for making time periods readable
*
* @author Aidan Lister <[email protected]>
* @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;
}
?>