1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/interval/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/interval/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\interval\helpers; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\i18n\Locale; |
13
|
|
|
use DateInterval; |
14
|
|
|
use DateTime; |
15
|
|
|
use DateTimeImmutable; |
16
|
|
|
use DateTimeZone; |
17
|
|
|
use yii\helpers\FormatConverter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class DateIntervalHelper |
24
|
|
|
{ |
25
|
|
|
// Constants |
26
|
|
|
// ========================================================================= |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Number of seconds in a minute. |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
const SECONDS_MINUTE = 60; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Number of seconds in an hour. |
37
|
|
|
* |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
const SECONDS_HOUR = 3600; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Number of seconds in a day. |
44
|
|
|
* |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
const SECONDS_DAY = 86400; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The number of seconds in a year. |
51
|
|
|
* |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
const SECONDS_YEAR = 31536000; |
55
|
|
|
|
56
|
|
|
// Public Methods |
57
|
|
|
// ========================================================================= |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param int $seconds The number of seconds |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public static function secondsToHumanTimeDuration(int $seconds): string |
65
|
|
|
{ |
66
|
|
|
$secondsInYear = self::SECONDS_YEAR; |
67
|
|
|
$secondsInDay = self::SECONDS_DAY; |
68
|
|
|
$secondsInHour = self::SECONDS_HOUR; |
69
|
|
|
$secondsInMinute = self::SECONDS_MINUTE; |
70
|
|
|
|
71
|
|
|
$years = floor($seconds / $secondsInYear); |
72
|
|
|
$seconds %= $secondsInYear; |
73
|
|
|
|
74
|
|
|
$days = floor($seconds / $secondsInDay); |
75
|
|
|
$seconds %= $secondsInDay; |
76
|
|
|
|
77
|
|
|
$hours = floor($seconds / $secondsInHour); |
78
|
|
|
$seconds %= $secondsInHour; |
79
|
|
|
|
80
|
|
|
$minutes = floor($seconds / $secondsInMinute); |
81
|
|
|
$seconds %= $secondsInMinute; |
82
|
|
|
|
83
|
|
|
$timeComponents = []; |
84
|
|
|
|
85
|
|
|
if ($years) { |
86
|
|
|
$timeComponents[] = $years.' '.($years == 1 ? Craft::t('app', 'year') : Craft::t('app', 'years')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($days) { |
90
|
|
|
$timeComponents[] = $days.' '.($days == 1 ? Craft::t('app', 'day') : Craft::t('app', 'days')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($hours) { |
94
|
|
|
$timeComponents[] = $hours.' '.($hours == 1 ? Craft::t('app', 'hour') : Craft::t('app', 'hours')); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($minutes) { |
98
|
|
|
$timeComponents[] = $minutes.' '.($minutes == 1 ? Craft::t('app', 'minute') : Craft::t('app', 'minutes')); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($seconds) { |
102
|
|
|
$timeComponents[] = $seconds.' '.($seconds == 1 ? Craft::t('app', 'second') : Craft::t('app', 'seconds')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return implode(', ', $timeComponents); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the interval in a human-friendly string. |
110
|
|
|
* |
111
|
|
|
* @param DateInterval $dateInterval |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public static function humanDurationFromInterval(DateInterval $dateInterval): string |
116
|
|
|
{ |
117
|
|
|
$timeComponents = []; |
118
|
|
|
|
119
|
|
|
if ($dateInterval->invert) { |
120
|
|
|
$timeComponents[] = '-'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($dateInterval->y) { |
124
|
|
|
$timeComponents[] = $dateInterval->y.' '. |
125
|
|
|
($dateInterval->y == 1 ? Craft::t('app', 'year') : Craft::t('app', 'years')); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($dateInterval->m) { |
129
|
|
|
$timeComponents[] = $dateInterval->m.' '. |
130
|
|
|
($dateInterval->m == 1 ? Craft::t('app', 'month') : Craft::t('app', 'months')); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($dateInterval->d) { |
134
|
|
|
$timeComponents[] = $dateInterval->d.' '. |
135
|
|
|
($dateInterval->d == 1 ? Craft::t('app', 'day') : Craft::t('app', 'days')); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($dateInterval->h) { |
139
|
|
|
$timeComponents[] = $dateInterval->h.' '. |
140
|
|
|
($dateInterval->h == 1 ? Craft::t('app', 'hour') : Craft::t('app', 'hours')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($dateInterval->i) { |
144
|
|
|
$timeComponents[] = $dateInterval->i.' '. |
145
|
|
|
($dateInterval->i == 1 ? Craft::t('app', 'minute') : Craft::t('app', 'minutes')); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($dateInterval->s) { |
149
|
|
|
$timeComponents[] = $dateInterval->s.' '. |
150
|
|
|
($dateInterval->s == 1 ? Craft::t('app', 'second') : Craft::t('app', 'seconds')); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return implode('', $timeComponents); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Creates a DateInterval object based on a given number of seconds. |
158
|
|
|
* |
159
|
|
|
* @param int $seconds |
160
|
|
|
* |
161
|
|
|
* @return DateInterval |
162
|
|
|
*/ |
163
|
|
|
public static function secondsToInterval(int $seconds): DateInterval |
164
|
|
|
{ |
165
|
|
|
return DateInterval::createFromDateString( |
166
|
|
|
self::secondsToHumanTimeDuration($seconds) |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns the number of seconds that a given DateInterval object spans. |
172
|
|
|
* |
173
|
|
|
* @param DateInterval $dateInterval |
174
|
|
|
* |
175
|
|
|
* @return int |
176
|
|
|
*/ |
177
|
|
|
public static function intervalToSeconds(DateInterval $dateInterval): int |
178
|
|
|
{ |
179
|
|
|
$reference = new DateTimeImmutable(); |
180
|
|
|
$endTime = $reference->add($dateInterval); |
181
|
|
|
|
182
|
|
|
return $endTime->getTimestamp() - $reference->getTimestamp(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns true if interval string is a valid interval. |
187
|
|
|
* |
188
|
|
|
* @param string $intervalString |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
public static function isValidIntervalString(string $intervalString): bool |
193
|
|
|
{ |
194
|
|
|
$interval = DateInterval::createFromDateString($intervalString); |
195
|
|
|
|
196
|
|
|
return $interval->s != 0 || |
197
|
|
|
$interval->i != 0 || |
198
|
|
|
$interval->h != 0 || |
199
|
|
|
$interval->d != 0 || |
200
|
|
|
$interval->m != 0 || |
201
|
|
|
$interval->y != 0; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|