Conditions | 9 |
Paths | 6 |
Total Lines | 36 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | public static function formatTime(int $duration, int $precision = 1): string |
||
8 | { |
||
9 | static $formats = [ |
||
10 | [0, '< 1 sec'], |
||
11 | [1, '1 sec'], |
||
12 | [2, 'secs', 1], |
||
13 | [60, '1 min'], |
||
14 | [120, 'mins', 60], |
||
15 | [3600, '1 hr'], |
||
16 | [7200, 'hrs', 3600], |
||
17 | [86400, '1 day'], |
||
18 | [172800, 'days', 86400], |
||
19 | ]; |
||
20 | |||
21 | foreach ($formats as $index => $format) { |
||
22 | if ($duration >= $format[0]) { |
||
23 | if ((isset($formats[$index + 1]) && $duration < $formats[$index + 1][0]) |
||
24 | || $index === \count($formats) - 1 |
||
25 | ) { |
||
26 | if (2 === \count($format)) { |
||
27 | return $format[1]; |
||
28 | } |
||
29 | |||
30 | --$precision; |
||
31 | |||
32 | $tmp = (int) floor($duration / $format[2]); |
||
33 | if (0 === $precision || 0 === $duration - ($tmp * $format[2])) { |
||
34 | return $tmp.' '.$format[1]; |
||
35 | } |
||
36 | |||
37 | return $tmp.' '.$format[1].' '.self::formatTime($duration - ($tmp * $format[2]), $precision); |
||
38 | } |
||
39 | } |
||
40 | } |
||
41 | |||
42 | return ''; |
||
43 | } |
||
61 |