Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
106 / 106 |
DateTime | |
100.00% |
1 / 1 |
|
100.00% |
11 / 11 |
33 | |
100.00% |
106 / 106 |
create | |
100.00% |
1 / 1 |
3 | |
100.00% |
6 / 6 |
|||
setDefaultDateFormat | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getDefaultDateFormat | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
hasDefaultDateFormat | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
setDefaultTimeFormat | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getDefaultTimeFormat | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
hasDefaultTimeFormat | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getTotal | |
100.00% |
1 / 1 |
8 | |
100.00% |
28 / 28 |
|||
getAverage | |
100.00% |
1 / 1 |
9 | |
100.00% |
29 / 29 |
|||
getWeekDates | |
100.00% |
1 / 1 |
4 | |
100.00% |
28 / 28 |
|||
__toString | |
100.00% |
1 / 1 |
3 | |
100.00% |
7 / 7 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2021 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Utils; |
15 | |
16 | use PhpParser\Builder\Property; |
17 | |
18 | /** |
19 | * Pop utils date-time helper class |
20 | * |
21 | * @category Pop |
22 | * @package Pop\Utils |
23 | * @author Nick Sagona, III <dev@nolainteractive.com> |
24 | * @copyright Copyright (c) 2009-2021 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
25 | * @license http://www.popphp.org/license New BSD License |
26 | * @version 1.2.0 |
27 | */ |
28 | class DateTime extends \DateTime |
29 | { |
30 | |
31 | /** |
32 | * Default date format |
33 | * @var string |
34 | */ |
35 | protected $defaultDateFormat = null; |
36 | |
37 | /** |
38 | * Default time format |
39 | * @var string |
40 | */ |
41 | protected $defaultTimeFormat = null; |
42 | |
43 | /** |
44 | * Create a new DateTime object |
45 | * |
46 | * @param string $dateTime |
47 | * @param \DateTimeZone $timeZone |
48 | * @param string $defaultDateFormat |
49 | * @param string $defaultTimeFormat |
50 | * @return static |
51 | */ |
52 | public static function create($dateTime = 'now', \DateTimeZone $timeZone = null, $defaultDateFormat = null, $defaultTimeFormat = null) |
53 | { |
54 | $dt = new static($dateTime, $timeZone); |
55 | if (null !== $defaultDateFormat) { |
56 | $dt->setDefaultDateFormat($defaultDateFormat); |
57 | } |
58 | if (null !== $defaultTimeFormat) { |
59 | $dt->setDefaultTimeFormat($defaultTimeFormat); |
60 | } |
61 | |
62 | return $dt; |
63 | } |
64 | |
65 | /** |
66 | * Method to set the default date format |
67 | * |
68 | * @param string $defaultDateFormat |
69 | * @return static |
70 | */ |
71 | public function setDefaultDateFormat($defaultDateFormat) |
72 | { |
73 | $this->defaultDateFormat = $defaultDateFormat; |
74 | return $this; |
75 | } |
76 | |
77 | /** |
78 | * Method to get the default date format |
79 | * |
80 | * @return string |
81 | */ |
82 | public function getDefaultDateFormat() |
83 | { |
84 | return $this->defaultDateFormat; |
85 | } |
86 | |
87 | /** |
88 | * Method to see if the object as a default date format |
89 | * |
90 | * @return boolean |
91 | */ |
92 | public function hasDefaultDateFormat() |
93 | { |
94 | return !empty($this->defaultDateFormat); |
95 | } |
96 | |
97 | /** |
98 | * Method to set the default time format |
99 | * |
100 | * @param string $defaultTimeFormat |
101 | * @return static |
102 | */ |
103 | public function setDefaultTimeFormat($defaultTimeFormat) |
104 | { |
105 | $this->defaultTimeFormat = $defaultTimeFormat; |
106 | return $this; |
107 | } |
108 | |
109 | /** |
110 | * Method to get the default time format |
111 | * |
112 | * @return string |
113 | */ |
114 | public function getDefaultTimeFormat() |
115 | { |
116 | return $this->defaultTimeFormat; |
117 | } |
118 | |
119 | /** |
120 | * Method to see if the object as a default time format |
121 | * |
122 | * @return boolean |
123 | */ |
124 | public function hasDefaultTimeFormat() |
125 | { |
126 | return !empty($this->defaultTimeFormat); |
127 | } |
128 | |
129 | /** |
130 | * Method to get total time from array of multiple time values in HH:MM:SS format |
131 | * |
132 | * Standard hh:mm:ss format string is '%H:%I:%S' |
133 | * |
134 | * @param array $times |
135 | * @param string $format |
136 | * @param boolean $secondsOnly |
137 | * @return \DateInterval|string |
138 | */ |
139 | public static function getTotal(array $times, $format = null, $secondsOnly = false) |
140 | { |
141 | $totalHours = 0; |
142 | $totalMinutes = 0; |
143 | $totalSeconds = 0; |
144 | |
145 | foreach ($times as $i => $time) { |
146 | if ($time instanceof \DateInterval) { |
147 | $hours = $time->format('%h'); |
148 | $minutes = $time->format('%i'); |
149 | $seconds = $time->format('%s'); |
150 | } else { |
151 | if (substr_count($time, ':') == 2) { |
152 | [$hours, $minutes, $seconds] = explode(':', $time); |
153 | } else { |
154 | $hours = 0; |
155 | [$minutes, $seconds] = explode(':', $time); |
156 | } |
157 | } |
158 | $totalHours += (int)$hours; |
159 | $totalMinutes += (int)$minutes; |
160 | $totalSeconds += (int)$seconds; |
161 | } |
162 | |
163 | if ($secondsOnly) { |
164 | $totalSeconds += (int)$totalHours * 3600; |
165 | $totalSeconds += (int)$totalMinutes * 60; |
166 | $intervalFormat = 'PT' . $totalSeconds . 'S'; |
167 | } else { |
168 | if ($totalSeconds > 60) { |
169 | $totalMinutes += floor($totalSeconds / 60); |
170 | $totalSeconds = $totalSeconds % 60; |
171 | } |
172 | if ($totalMinutes > 60) { |
173 | $totalHours += floor($totalMinutes / 60); |
174 | $totalMinutes = $totalMinutes % 60; |
175 | } |
176 | $intervalFormat = 'PT' . (int)$totalHours . 'H' . (int)$totalMinutes . 'M' . (int)$totalSeconds . 'S'; |
177 | } |
178 | |
179 | $dateInterval = new \DateInterval($intervalFormat); |
180 | |
181 | return (null !== $format) ? $dateInterval->format($format) : $dateInterval; |
182 | } |
183 | |
184 | /** |
185 | * Method to get average time from array of multiple time values in HH:MM:SS format |
186 | * |
187 | * Standard hh:mm:ss format string is '%H:%I:%S' |
188 | * |
189 | * @param array $times |
190 | * @param string $format |
191 | * @param boolean $secondsOnly |
192 | * @return \DateInterval|string |
193 | */ |
194 | public static function getAverage(array $times, $format = null, $secondsOnly = false) |
195 | { |
196 | $total = static::getTotal($times, null, true); |
197 | $totalTime = $total->s; |
198 | $averageTime = round(($totalTime / count($times)), 2); |
199 | $hh = 0; |
200 | $mm = 0; |
201 | $ss = 0; |
202 | |
203 | if ($averageTime >= 3600) { |
204 | $hh = floor($averageTime / 3600); |
205 | $mins = $averageTime - ($hh * 3600); |
206 | $mm = floor($mins / 60); |
207 | $ss = ($mins - ($mm * 60)) % 60; |
208 | } else if (($averageTime < 3600) && ($averageTime >= 60)) { |
209 | $mm = floor($averageTime / 60); |
210 | $ss = ($averageTime % 60); |
211 | } else { |
212 | $ss = $averageTime; |
213 | } |
214 | |
215 | if ($secondsOnly) { |
216 | $totalSeconds = 0; |
217 | $totalSeconds += (int)$hh * 3600; |
218 | $totalSeconds += (int)$mm * 60; |
219 | $intervalFormat = 'PT' . $totalSeconds . 'S'; |
220 | } else { |
221 | $intervalFormat = 'PT'; |
222 | if ($hh != 0) { |
223 | $intervalFormat .= (int)$hh . 'H'; |
224 | } |
225 | if ($mm != 0) { |
226 | $intervalFormat .= (int)$mm . 'M'; |
227 | } |
228 | if ($ss != 0) { |
229 | $intervalFormat .= (int)$ss . 'S'; |
230 | } |
231 | } |
232 | |
233 | $dateInterval = new \DateInterval($intervalFormat); |
234 | |
235 | return (null !== $format) ? $dateInterval->format($format) : $dateInterval; |
236 | } |
237 | |
238 | /** |
239 | * Method to get dates of a week |
240 | * |
241 | * @param int $week |
242 | * @param int $year |
243 | * @param string $format |
244 | * @return array |
245 | */ |
246 | public static function getWeekDates($week = null, $year = null, $format = null) |
247 | { |
248 | if (null === $week) { |
249 | $week = date('W'); |
250 | } |
251 | if (null === $year) { |
252 | $year = date('Y'); |
253 | } |
254 | |
255 | $today = new static('today'); |
256 | $sunday = clone $today->setISODate($year, $week, 0); |
257 | $monday = clone $today->setISODate($year, $week, 1); |
258 | $tuesday = clone $today->setISODate($year, $week, 2); |
259 | $wednesday = clone $today->setISODate($year, $week, 3); |
260 | $thursday = clone $today->setISODate($year, $week, 4); |
261 | $friday = clone $today->setISODate($year, $week, 5); |
262 | $saturday = clone $today->setISODate($year, $week, 6); |
263 | |
264 | if (null !== $format) { |
265 | $weekDates = [ |
266 | 0 => $sunday->format($format), |
267 | 1 => $monday->format($format), |
268 | 2 => $tuesday->format($format), |
269 | 3 => $wednesday->format($format), |
270 | 4 => $thursday->format($format), |
271 | 5 => $friday->format($format), |
272 | 6 => $saturday->format($format), |
273 | ]; |
274 | } else { |
275 | $weekDates = [ |
276 | 0 => $sunday, |
277 | 1 => $monday, |
278 | 2 => $tuesday, |
279 | 3 => $wednesday, |
280 | 4 => $thursday, |
281 | 5 => $friday, |
282 | 6 => $saturday, |
283 | ]; |
284 | } |
285 | |
286 | return $weekDates; |
287 | } |
288 | |
289 | /** |
290 | * __toString method |
291 | * |
292 | * @return string |
293 | */ |
294 | public function __toString() |
295 | { |
296 | $string = ''; |
297 | |
298 | if (!empty($this->defaultDateFormat)) { |
299 | $format = $this->defaultDateFormat; |
300 | |
301 | if (!empty($this->defaultTimeFormat)) { |
302 | $format .= ' ' . $this->defaultTimeFormat; |
303 | } |
304 | |
305 | $string = $this->format($format); |
306 | } |
307 | |
308 | return $string; |
309 | } |
310 | |
311 | } |
312 |