|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This class is an adapted version of DateInterval, original work by Mochamad Gufron. |
|
7
|
|
|
* |
|
8
|
|
|
* A simple class to convert time duration to a human readable string |
|
9
|
|
|
* |
|
10
|
|
|
* @class Duration |
|
11
|
|
|
* |
|
12
|
|
|
* @author Mochamad Gufron |
|
13
|
|
|
* |
|
14
|
|
|
* @link mgufron.com |
|
15
|
|
|
* @contact [email protected] |
|
16
|
|
|
*/ |
|
17
|
|
|
class Duration |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var int timestamp interval |
|
21
|
|
|
*/ |
|
22
|
|
|
public $interval = 0; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var int save interval when using format mode |
|
26
|
|
|
*/ |
|
27
|
|
|
private $tempInterval = 0; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string used format for forming the output |
|
31
|
|
|
*/ |
|
32
|
|
|
public $format = ''; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param int $interval timestamp interval |
|
36
|
|
|
* @param \DateTime $interval the first date |
|
37
|
|
|
*/ |
|
38
|
9 |
|
public function __construct($interval = 0) |
|
39
|
|
|
{ |
|
40
|
9 |
|
if ($interval instanceof \DateTime) { |
|
41
|
|
|
$first_interval = strtotime($interval->format('Y-m-d H:i:s')); |
|
42
|
|
|
$last_interval = strtotime($interval->format('Y-m-d H:i:s')); |
|
43
|
|
|
$interval = $last_interval - $first_interval; |
|
44
|
|
|
} |
|
45
|
9 |
|
$this->interval = $interval; |
|
46
|
9 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param int $interval set current time interval |
|
50
|
|
|
* |
|
51
|
|
|
* @return self object |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setInterval($interval) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->interval = $interval; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $format set current format |
|
62
|
|
|
* |
|
63
|
|
|
* @return self object |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setFormat($format) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->format = $format; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param int $round_method rounding method after we reveal the readable |
|
74
|
|
|
* interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN |
|
75
|
|
|
* |
|
76
|
|
|
* @return float in seconds |
|
77
|
|
|
*/ |
|
78
|
5 |
|
public function getSeconds($round_method = null) |
|
79
|
|
|
{ |
|
80
|
5 |
|
$result = $this->interval / 1000; |
|
81
|
|
|
|
|
82
|
5 |
|
return $this->round($result, $round_method); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param int $round_method rounding method after we reveal the readable |
|
87
|
|
|
* interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN |
|
88
|
|
|
* |
|
89
|
|
|
* @return float in minutes |
|
90
|
|
|
*/ |
|
91
|
5 |
|
public function getMinutes($round_method = null) |
|
92
|
|
|
{ |
|
93
|
5 |
|
$result = $this->interval / (1000 * 60); |
|
94
|
|
|
|
|
95
|
5 |
|
return $this->round($result, $round_method); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param int $round_method rounding method after we reveal the readable |
|
100
|
|
|
* interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN |
|
101
|
|
|
* |
|
102
|
|
|
* @return float in hours |
|
103
|
|
|
*/ |
|
104
|
5 |
|
public function getHours($round_method = null) |
|
105
|
|
|
{ |
|
106
|
5 |
|
$result = $this->interval / (1000 * 60 * 60); |
|
107
|
|
|
|
|
108
|
5 |
|
return $this->round($result, $round_method); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $format rounding method after we reveal the readable |
|
113
|
|
|
* interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN |
|
114
|
|
|
* |
|
115
|
|
|
* @return well formatted output |
|
116
|
|
|
*/ |
|
117
|
3 |
|
public function format($format = []) |
|
118
|
|
|
{ |
|
119
|
3 |
|
$this->tempInterval = $this->interval; |
|
120
|
3 |
|
$hours = $this->getHours(PHP_ROUND_HALF_DOWN); |
|
121
|
3 |
|
$this->interval = $this->tempInterval % (1000 * 60 * 60); |
|
122
|
3 |
|
$minutes = $this->getMinutes(PHP_ROUND_HALF_DOWN); |
|
123
|
3 |
|
$this->interval = $this->tempInterval % (1000 * 60); |
|
124
|
3 |
|
$seconds = $this->getSeconds(PHP_ROUND_HALF_DOWN); |
|
125
|
3 |
|
$this->interval = $this->tempInterval; |
|
126
|
3 |
|
if (is_string($format)) { |
|
127
|
1 |
|
$result = strtr($format, [ |
|
128
|
1 |
|
'{hours}' => $hours, |
|
129
|
1 |
|
'{minutes}' => $minutes, |
|
130
|
1 |
|
'{seconds}' => $seconds, |
|
131
|
|
|
]); |
|
132
|
|
|
} else { |
|
133
|
2 |
|
if ($seconds <= 0) { |
|
134
|
2 |
|
$format['{seconds}'] = ''; |
|
135
|
|
|
} |
|
136
|
2 |
|
if ($minutes <= 0) { |
|
137
|
2 |
|
$format['{minutes}'] = ''; |
|
138
|
|
|
} |
|
139
|
2 |
|
if ($hours <= 0) { |
|
140
|
1 |
|
$format['{hours}'] = ''; |
|
141
|
|
|
} |
|
142
|
2 |
|
$format['{seconds}'] = strtr($format['{seconds}'], ['{seconds}' => $seconds]); |
|
143
|
2 |
|
$format['{minutes}'] = strtr($format['{minutes}'], ['{minutes}' => $minutes]); |
|
144
|
2 |
|
$format['{hours}'] = strtr($format['{hours}'], ['{hours}' => $hours]); |
|
145
|
2 |
|
$result = trim(strtr($format['template'], $format)); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
3 |
|
$result = $this->fixSingulars($result); |
|
149
|
|
|
|
|
150
|
3 |
|
return $result; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param int $round_method rounding method after we reveal the readable |
|
155
|
|
|
* interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN |
|
156
|
|
|
* |
|
157
|
|
|
* @return float current number of the result. if it is using $round_method, |
|
158
|
|
|
* it will rounded up or down |
|
159
|
|
|
*/ |
|
160
|
9 |
|
private function round($result, $round_method = null) |
|
161
|
|
|
{ |
|
162
|
9 |
|
if ($round_method === PHP_ROUND_HALF_UP) { |
|
163
|
3 |
|
$result = ceil($result); |
|
164
|
9 |
|
} elseif ($round_method === PHP_ROUND_HALF_DOWN) { |
|
165
|
6 |
|
$result = floor($result); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
9 |
|
return $result; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return well formatted output |
|
173
|
|
|
*/ |
|
174
|
|
|
public function __toString() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->format($this->format); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Fix singulars of time units |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $string |
|
183
|
|
|
* @return string Formatted string with replaced plurals to singular |
|
184
|
|
|
*/ |
|
185
|
3 |
|
protected function fixSingulars($string) |
|
186
|
|
|
{ |
|
187
|
3 |
|
$plurals = ['/^1 hours/', '/^1 minutes/', '/^1 seconds/']; |
|
188
|
3 |
|
$singulars = ['1 hour', '1 minute', '1 second']; |
|
189
|
|
|
|
|
190
|
3 |
|
return preg_replace($plurals, $singulars, $string); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|