|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\Helpers\Time; |
|
4
|
|
|
|
|
5
|
|
|
use Sfneal\Actions\AbstractService; |
|
6
|
|
|
|
|
7
|
|
|
class TimeConverter extends AbstractService |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var float Number of hours |
|
11
|
|
|
*/ |
|
12
|
|
|
private $hours; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var float Number of minutes |
|
16
|
|
|
*/ |
|
17
|
|
|
private $minutes; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var float Number of seconds |
|
21
|
|
|
*/ |
|
22
|
|
|
private $seconds; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Retrieve number of hours as a float. |
|
26
|
|
|
* |
|
27
|
|
|
* @return float |
|
28
|
|
|
*/ |
|
29
|
|
|
public function hours(): float |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->hours ?? 0; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Retrieve number of minutes as a float. |
|
36
|
|
|
* |
|
37
|
|
|
* @return float |
|
38
|
|
|
*/ |
|
39
|
|
|
public function minutes(): float |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->minutes ?? 0; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Retrieve number of seconds as a float. |
|
46
|
|
|
* |
|
47
|
|
|
* @return float |
|
48
|
|
|
*/ |
|
49
|
|
|
public function seconds(): float |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->seconds ?? 0; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set the number of hours. |
|
56
|
|
|
* |
|
57
|
|
|
* @param float $hours |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setHours(float $hours): self |
|
61
|
|
|
{ |
|
62
|
|
|
$this->hours = $hours; |
|
63
|
|
|
$this->minutes = $hours * 60; |
|
64
|
|
|
$this->seconds = null; |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the number of minutes. |
|
71
|
|
|
* |
|
72
|
|
|
* @param float $minutes |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setMinutes(float $minutes): self |
|
76
|
|
|
{ |
|
77
|
|
|
$this->hours = $minutes / 60; |
|
78
|
|
|
$this->minutes = $minutes; |
|
79
|
|
|
$this->seconds = null; |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set the number of seconds. |
|
86
|
|
|
* |
|
87
|
|
|
* @param float $seconds |
|
88
|
|
|
* @return $this |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setSeconds(float $seconds): self |
|
91
|
|
|
{ |
|
92
|
|
|
$this->hours = $seconds / 3600; |
|
93
|
|
|
$this->minutes = $seconds / 60; |
|
94
|
|
|
$this->seconds = $seconds; |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Retrieve time converted to hours string. |
|
101
|
|
|
* |
|
102
|
|
|
* @param bool $include_seconds |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getHours(bool $include_seconds = true): string |
|
106
|
|
|
{ |
|
107
|
|
|
// Calculate remaining minutes |
|
108
|
|
|
$minutes = $this->minutes % 60; |
|
109
|
|
|
|
|
110
|
|
|
if ($include_seconds && isset($this->seconds)) { |
|
111
|
|
|
// Calculate number of remaining seconds if there are $seconds |
|
112
|
|
|
$seconds = $this->seconds - (floor($this->hours) * 3600) - ($minutes * 60); |
|
113
|
|
|
|
|
114
|
|
|
// Join & zero value time values (with seconds) |
|
115
|
|
|
return self::transform($this->hours, $minutes, $seconds); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Join & zero value time values |
|
119
|
|
|
return self::transform($this->hours, $minutes); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Join arrays of times to create datetime like strings. |
|
124
|
|
|
* |
|
125
|
|
|
* - prepend '0' to single digit integers |
|
126
|
|
|
* - separate time values with ':' chars |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $times |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
private static function transform(...$times): string |
|
132
|
|
|
{ |
|
133
|
|
|
// Floor times & prepend "0" to single digit (< 10) |
|
134
|
|
|
return join(':', array_map(function (float $time) { |
|
135
|
|
|
return self::zeroFill(floor($time)); |
|
|
|
|
|
|
136
|
|
|
}, $times)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Prepend a "0" to a single digit integer. |
|
141
|
|
|
* |
|
142
|
|
|
* @param int $int |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
private static function zeroFill(int $int): string |
|
146
|
|
|
{ |
|
147
|
|
|
return (self::isSingleDigitInt($int) ? '0' : '').$int; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Determine if an integer is a single digit (< 10). |
|
152
|
|
|
* |
|
153
|
|
|
* @param int $int |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
|
|
private static function isSingleDigitInt(int $int): bool |
|
157
|
|
|
{ |
|
158
|
|
|
return strlen((string) $int) == 1; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|