-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimestr.php
55 lines (48 loc) · 1.52 KB
/
timestr.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
53
54
<?
global $wordSets;
$wordSets['long'] = array(
's' => ' Second',
'm' => ' Minute',
'h' => ' Hour',
'd' => ' Day',
'plurality' => true
);
$wordSets['medium'] = array(
's' => ' Sec',
'm' => ' Min',
'h' => ' Hr',
'd' => ' Day',
'plurality' => true
);
$wordSets['short'] = array(
's' => 'S',
'm' => 'M',
'h' => 'H',
'd' => 'D',
'plurality' => false
);
function timestr($secs, $wordset = null) {
global $wordSets;
if ($wordset && $wordSets[$wordset]) $wordset = $wordSets[$wordset];
if ($secs <= 59) {
$str = $secs . timestr_word($wordset, $secs, 's');
} elseif($secs <= 3599) {
$tmp = floor($secs / 60);
$remain = floor($secs - ($tmp * 60));
$str = $tmp . timestr_word($wordset, $tmp, 'm') . " " . $remain . timestr_word($wordset, $remain, 's');
} elseif($secs <= 86399) {
$tmp = floor(($secs / 60) / 60);
$remain = floor(($secs / 60) - ($tmp * 60));
$str = $tmp . timestr_word($wordset, $tmp, 'h') . " " . $remain . timestr_word($wordset, $remain, 'm');
} elseif($secs >= 86400) {
$tmp = floor((($secs / 60) / 60) / 24);
$remain = floor((($secs / 60) - (($tmp * 24) * 60)) / 60);
$str = $tmp . timestr_word($wordset, $tmp, 'd') . " " . $remain . timestr_word($wordset, $remain, 'h');
} else { $str = "Invalid Time Argument: $secs"; }
return($str);
}
function timestr_word($wordset, $n, $type = null) {
if (!$wordset) $wordset = array('s'=>' Second','m'=>' Minute','h'=>' Hour','d'=>' Day','plurality'=>true);
if ($wordset['plurality'] && ($n != 1)) $plural = 's';
return $wordset[$type] . $plural;
}