|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @Auther: bhief |
|
5
|
|
|
* @Version: 1.0 |
|
6
|
|
|
* |
|
7
|
|
|
* Time manipulation class. Used for converting seconds -> time and getting x seconds ago, eg. |
|
8
|
|
|
* |
|
9
|
|
|
**/ |
|
10
|
|
|
class time_helper { |
|
11
|
|
|
function time_elapsed_string($datetime, $full = false) { |
|
|
|
|
|
|
12
|
|
|
global $cLang; |
|
13
|
|
|
$now = new DateTime; |
|
14
|
|
|
$ago = new DateTime($datetime); |
|
15
|
|
|
$diff = $now->diff($ago); |
|
16
|
|
|
|
|
17
|
|
|
$diff->w = floor($diff->d / 7); |
|
|
|
|
|
|
18
|
|
|
$diff->d -= $diff->w * 7; |
|
19
|
|
|
|
|
20
|
|
|
if(!isset($cLang)) { |
|
21
|
|
|
$string = array( |
|
22
|
|
|
'y' => 'year', |
|
23
|
|
|
'm' => 'month', |
|
24
|
|
|
'w' => 'week', |
|
25
|
|
|
'd' => 'day', |
|
26
|
|
|
'h' => 'hour', |
|
27
|
|
|
'i' => 'minute', |
|
28
|
|
|
's' => 'second', |
|
29
|
|
|
); |
|
30
|
|
|
} else { |
|
31
|
|
|
$string = array( |
|
32
|
|
|
'y' => $cLang['year'], |
|
33
|
|
|
'm' => $cLang['month'], |
|
34
|
|
|
'w' => $cLang['week'], |
|
35
|
|
|
'd' => $cLang['day'], |
|
36
|
|
|
'h' => $cLang['hour'], |
|
37
|
|
|
'i' => $cLang['minute'], |
|
38
|
|
|
's' => $cLang['second'], |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if(!isset($cLang)) { |
|
43
|
|
|
foreach ($string as $k => &$v) { |
|
44
|
|
|
if ($diff->$k) { |
|
45
|
|
|
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : ''); |
|
46
|
|
|
} else { |
|
47
|
|
|
unset($string[$k]); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} else { |
|
51
|
|
|
foreach ($string as $k => &$v) { |
|
52
|
|
|
if ($diff->$k) { |
|
53
|
|
|
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? $cLang['pluralTimeRight'] : ''); |
|
54
|
|
|
} else { |
|
55
|
|
|
unset($string[$k]); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if(!isset($cLang)) { |
|
61
|
|
|
if (!$full) $string = array_slice($string, 0, 1); |
|
62
|
|
|
return $string ? implode(', ', $string) . ' ago' : 'just now'; |
|
63
|
|
|
} else { |
|
64
|
|
|
if (!$full) $string = array_slice($string, 0, 1); |
|
65
|
|
|
return $string ? implode($cLang['agoLeft'], $string) . " " . " " . $cLang['agoRight'] : $cLang['justNow']; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
function timestamp(int $seconds) { |
|
|
|
|
|
|
71
|
|
|
if ($seconds > 60*60*24) { |
|
72
|
|
|
// over a day |
|
73
|
|
|
return sprintf("%d:%s:%s:%s", |
|
74
|
|
|
floor($seconds/60/60/24), // Days |
|
75
|
|
|
str_pad( floor($seconds/60/60%24), 2, "0", STR_PAD_LEFT), // Hours |
|
76
|
|
|
str_pad( floor($seconds/60%60), 2, "0", STR_PAD_LEFT), // Minutes |
|
77
|
|
|
str_pad( $seconds%60, 2, "0", STR_PAD_LEFT) // Seconds |
|
78
|
|
|
); |
|
79
|
|
|
} else if ($seconds > 60*60) { |
|
80
|
|
|
// over an hour |
|
81
|
|
|
return sprintf("%d:%s:%s", |
|
82
|
|
|
floor($seconds/60/60), // Hours |
|
83
|
|
|
str_pad(floor($seconds/60%60), 2, "0", STR_PAD_LEFT), // Minutes |
|
84
|
|
|
str_pad( $seconds%60, 2, "0", STR_PAD_LEFT) // Seconds |
|
85
|
|
|
); |
|
86
|
|
|
} else { |
|
87
|
|
|
// less than an hour |
|
88
|
|
|
return sprintf("%d:%s", |
|
89
|
|
|
floor($seconds/60), // Minutes |
|
90
|
|
|
str_pad( $seconds%60, 2, "0", STR_PAD_LEFT) // Seconds |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
?> |
|
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.