|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* League.Period (https://period.thephpleague.com) |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Ignace Nyamagana Butera <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace League\Period; |
|
15
|
|
|
|
|
16
|
|
|
use DateInterval; |
|
17
|
|
|
use DatePeriod; |
|
18
|
|
|
use DateTimeImmutable; |
|
19
|
|
|
use function is_int; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Returns a DateTimeImmutable object. |
|
23
|
|
|
* |
|
24
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
25
|
|
|
* |
|
26
|
|
|
* @deprecated deprecated since version 4.2 |
|
27
|
|
|
* @see Datepoint::create |
|
28
|
|
|
* |
|
29
|
|
|
* @param mixed $datepoint a datepoint |
|
30
|
|
|
*/ |
|
31
|
|
|
function datepoint($datepoint): DateTimeImmutable |
|
32
|
|
|
{ |
|
33
|
18 |
|
if ($datepoint instanceof DateTimeImmutable) { |
|
34
|
3 |
|
return $datepoint; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
15 |
|
return Datepoint::create($datepoint); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns a DateInval object. |
|
42
|
|
|
* |
|
43
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
44
|
|
|
* |
|
45
|
|
|
* @deprecated deprecated since version 4.2 |
|
46
|
|
|
* @see Duration::create |
|
47
|
|
|
* |
|
48
|
|
|
* @param mixed $duration a Duration |
|
49
|
|
|
*/ |
|
50
|
|
|
function duration($duration): DateInterval |
|
51
|
|
|
{ |
|
52
|
15 |
|
if ($duration instanceof DateInterval) { |
|
53
|
3 |
|
return $duration; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
12 |
|
return Duration::create($duration); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Creates new instance from a starting point and an interval. |
|
61
|
|
|
* |
|
62
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
63
|
|
|
* |
|
64
|
|
|
* @deprecated deprecated since version 4.2 |
|
65
|
|
|
* @see Period::after |
|
66
|
|
|
* |
|
67
|
|
|
* @param mixed $startDate the starting included datepoint |
|
68
|
|
|
* @param mixed $duration a Duration |
|
69
|
|
|
*/ |
|
70
|
|
|
function interval_after($startDate, $duration): Period |
|
71
|
|
|
{ |
|
72
|
27 |
|
return Period::after($startDate, $duration); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Creates new instance from a ending excluded datepoint and an interval. |
|
77
|
|
|
* |
|
78
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
79
|
|
|
* |
|
80
|
|
|
* @deprecated deprecated since version 4.2 |
|
81
|
|
|
* @see Period::before |
|
82
|
|
|
* |
|
83
|
|
|
* @param mixed $endDate the ending excluded datepoint |
|
84
|
|
|
* @param mixed $duration a Duration |
|
85
|
|
|
*/ |
|
86
|
|
|
function interval_before($endDate, $duration): Period |
|
87
|
|
|
{ |
|
88
|
12 |
|
return Period::before($endDate, $duration); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Creates new instance where the given duration is simultaneously |
|
93
|
|
|
* subtracted from and added to the datepoint. |
|
94
|
|
|
* |
|
95
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
96
|
|
|
* |
|
97
|
|
|
* @deprecated deprecated since version 4.2 |
|
98
|
|
|
* @see Period::around |
|
99
|
|
|
* |
|
100
|
|
|
* @param mixed $datepoint a datepoint at the center of the returned instance |
|
101
|
|
|
* @param mixed $duration a Duration |
|
102
|
|
|
*/ |
|
103
|
|
|
function interval_around($datepoint, $duration): Period |
|
104
|
|
|
{ |
|
105
|
6 |
|
return Period::around($datepoint, $duration); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Creates new instance from a DatePeriod. |
|
110
|
|
|
* |
|
111
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
112
|
|
|
* |
|
113
|
|
|
* @deprecated deprecated since version 4.2 |
|
114
|
|
|
* @see Period::fromDatePeriod |
|
115
|
|
|
*/ |
|
116
|
|
|
function interval_from_dateperiod(DatePeriod $datePeriod): Period |
|
117
|
|
|
{ |
|
118
|
6 |
|
return Period::fromDatePeriod($datePeriod); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Creates new instance for a specific year. |
|
123
|
|
|
* |
|
124
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
125
|
|
|
* |
|
126
|
|
|
* @deprecated deprecated since version 4.2 |
|
127
|
|
|
* @see Period::fromYear |
|
128
|
|
|
* @see Datepoint::getYear |
|
129
|
|
|
* |
|
130
|
|
|
* @param mixed $year_or_datepoint a year as an int or a datepoint |
|
131
|
|
|
*/ |
|
132
|
|
|
function year($year_or_datepoint): Period |
|
133
|
|
|
{ |
|
134
|
9 |
|
if (is_int($year_or_datepoint)) { |
|
135
|
6 |
|
return Period::fromYear($year_or_datepoint); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
6 |
|
return Datepoint::create($year_or_datepoint)->getYear(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Creates new instance for a specific ISO year. |
|
143
|
|
|
* |
|
144
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
145
|
|
|
* |
|
146
|
|
|
* @deprecated deprecated since version 4.2 |
|
147
|
|
|
* @see Period::fromIsoYear |
|
148
|
|
|
* @see Datepoint::getIsoYear |
|
149
|
|
|
* |
|
150
|
|
|
* @param mixed $year_or_datepoint an iso year as an int or a datepoint |
|
151
|
|
|
*/ |
|
152
|
|
|
function iso_year($year_or_datepoint): Period |
|
153
|
|
|
{ |
|
154
|
3 |
|
if (is_int($year_or_datepoint)) { |
|
155
|
3 |
|
return Period::fromIsoYear($year_or_datepoint); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
3 |
|
return Datepoint::create($year_or_datepoint)->getIsoYear(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Creates new instance for a specific semester in a given year. |
|
163
|
|
|
* |
|
164
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
165
|
|
|
* |
|
166
|
|
|
* @deprecated deprecated since version 4.2 |
|
167
|
|
|
* @see Period::fromSemester |
|
168
|
|
|
* @see Datepoint::getSemester |
|
169
|
|
|
* |
|
170
|
|
|
* @param mixed $year_or_datepoint a year as an int or a datepoint |
|
171
|
|
|
*/ |
|
172
|
|
|
function semester($year_or_datepoint, int $semester = 1): Period |
|
173
|
|
|
{ |
|
174
|
12 |
|
if (is_int($year_or_datepoint)) { |
|
175
|
9 |
|
return Period::fromSemester($year_or_datepoint, $semester); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
6 |
|
return Datepoint::create($year_or_datepoint)->getSemester(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Creates new instance for a specific quarter in a given year. |
|
183
|
|
|
* |
|
184
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
185
|
|
|
* |
|
186
|
|
|
* @deprecated deprecated since version 4.2 |
|
187
|
|
|
* @see Period::fromQuarter |
|
188
|
|
|
* @see Datepoint::getQuarter |
|
189
|
|
|
* |
|
190
|
|
|
* @param mixed $year_or_datepoint an iso year as an int or a datepoint |
|
191
|
|
|
*/ |
|
192
|
|
|
function quarter($year_or_datepoint, int $quarter = 1): Period |
|
193
|
|
|
{ |
|
194
|
12 |
|
if (is_int($year_or_datepoint)) { |
|
195
|
9 |
|
return Period::fromQuarter($year_or_datepoint, $quarter); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
6 |
|
return Datepoint::create($year_or_datepoint)->getQuarter(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Creates new instance for a specific year and month. |
|
203
|
|
|
* |
|
204
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
205
|
|
|
* |
|
206
|
|
|
* @deprecated deprecated since version 4.2 |
|
207
|
|
|
* @see Period::fromMonth |
|
208
|
|
|
* @see Datepoint::getMonth |
|
209
|
|
|
* |
|
210
|
|
|
* @param mixed $year_or_datepoint a year as an int or a datepoint |
|
211
|
|
|
*/ |
|
212
|
|
|
function month($year_or_datepoint, int $month = 1): Period |
|
213
|
|
|
{ |
|
214
|
27 |
|
if (is_int($year_or_datepoint)) { |
|
215
|
21 |
|
return Period::fromMonth($year_or_datepoint, $month); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
9 |
|
return Datepoint::create($year_or_datepoint)->getMonth(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Creates new instance for a specific ISO8601 week. |
|
223
|
|
|
* |
|
224
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
225
|
|
|
* |
|
226
|
|
|
* @deprecated deprecated since version 4.2 |
|
227
|
|
|
* @see Period::fromIsoWeek |
|
228
|
|
|
* @see Datepoint::getIsoWeek |
|
229
|
|
|
* |
|
230
|
|
|
* @param mixed $year_or_datepoint an iso year as an int or a datepoint |
|
231
|
|
|
*/ |
|
232
|
|
|
function iso_week($year_or_datepoint, int $week = 1): Period |
|
233
|
|
|
{ |
|
234
|
12 |
|
if (is_int($year_or_datepoint)) { |
|
235
|
9 |
|
return Period::fromIsoWeek($year_or_datepoint, $week); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
6 |
|
return Datepoint::create($year_or_datepoint)->getIsoWeek(); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Creates new instance for a specific date. |
|
243
|
|
|
* |
|
244
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
245
|
|
|
* |
|
246
|
|
|
* @deprecated deprecated since version 4.2 |
|
247
|
|
|
* @see Period::fromIsoWeek |
|
248
|
|
|
* @see Datepoint::getDay |
|
249
|
|
|
* |
|
250
|
|
|
* The date is truncated so that the time range starts at midnight |
|
251
|
|
|
* according to the date timezone and last a full day. |
|
252
|
|
|
* |
|
253
|
|
|
* @param mixed $year_or_datepoint a year as an int or a datepoint |
|
254
|
|
|
*/ |
|
255
|
|
|
function day($year_or_datepoint, int $month = 1, int $day = 1): Period |
|
256
|
|
|
{ |
|
257
|
9 |
|
if (is_int($year_or_datepoint)) { |
|
258
|
6 |
|
return Period::fromDay($year_or_datepoint, $month, $day); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
6 |
|
return Datepoint::create($year_or_datepoint)->getDay(); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Creates new instance for a specific date and hour. |
|
266
|
|
|
* |
|
267
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
268
|
|
|
* |
|
269
|
|
|
* @deprecated deprecated since version 4.2 |
|
270
|
|
|
* @see Datepoint::getHour |
|
271
|
|
|
* |
|
272
|
|
|
* The starting datepoint represents the beginning of the hour |
|
273
|
|
|
* The interval is equal to 1 hour |
|
274
|
|
|
* |
|
275
|
|
|
* @param mixed $year_or_datepoint a year as an int or a datepoint |
|
276
|
|
|
*/ |
|
277
|
|
|
function hour($year_or_datepoint, int $month = 1, int $day = 1, int $hour = 0): Period |
|
278
|
|
|
{ |
|
279
|
9 |
|
if (is_int($year_or_datepoint)) { |
|
280
|
6 |
|
$startDate = (new DateTimeImmutable()) |
|
281
|
6 |
|
->setDate($year_or_datepoint, $month, $day) |
|
282
|
6 |
|
->setTime($hour, 0); |
|
283
|
|
|
|
|
284
|
6 |
|
return Period::after($startDate, new DateInterval('PT1H')); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
6 |
|
return Datepoint::create($year_or_datepoint)->getHour(); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Creates new instance for a specific date, hour and minute. |
|
292
|
|
|
* |
|
293
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
294
|
|
|
* |
|
295
|
|
|
* @deprecated deprecated since version 4.2 |
|
296
|
|
|
* @see Datepoint::getMinute |
|
297
|
|
|
* |
|
298
|
|
|
* The starting datepoint represents the beginning of the minute |
|
299
|
|
|
* The interval is equal to 1 minute |
|
300
|
|
|
* |
|
301
|
|
|
* @param mixed $year_or_datepoint a year as an int or a datepoint |
|
302
|
|
|
*/ |
|
303
|
|
|
function minute($year_or_datepoint, int $month = 1, int $day = 1, int $hour = 0, int $minute = 0): Period |
|
304
|
|
|
{ |
|
305
|
9 |
|
if (is_int($year_or_datepoint)) { |
|
306
|
6 |
|
$startDate = (new DateTimeImmutable()) |
|
307
|
6 |
|
->setDate($year_or_datepoint, $month, $day) |
|
308
|
6 |
|
->setTime($hour, $minute); |
|
309
|
|
|
|
|
310
|
6 |
|
return Period::after($startDate, new DateInterval('PT1M')); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
6 |
|
return Datepoint::create($year_or_datepoint)->getMinute(); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* Creates new instance for a specific date, hour, minute and second. |
|
318
|
|
|
* |
|
319
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
320
|
|
|
* |
|
321
|
|
|
* @deprecated deprecated since version 4.2 |
|
322
|
|
|
* @see Datepoint::getSecond |
|
323
|
|
|
* |
|
324
|
|
|
* The starting datepoint represents the beginning of the second |
|
325
|
|
|
* The interval is equal to 1 second |
|
326
|
|
|
* |
|
327
|
|
|
* @param mixed $year_or_datepoint a year as an int or a datepoint |
|
328
|
|
|
*/ |
|
329
|
|
|
function second( |
|
330
|
|
|
$year_or_datepoint, |
|
331
|
|
|
int $month = 1, |
|
332
|
|
|
int $day = 1, |
|
333
|
|
|
int $hour = 0, |
|
334
|
|
|
int $minute = 0, |
|
335
|
|
|
int $second = 0 |
|
336
|
|
|
): Period { |
|
337
|
9 |
|
if (is_int($year_or_datepoint)) { |
|
338
|
6 |
|
$startDate = (new DateTimeImmutable()) |
|
339
|
6 |
|
->setDate($year_or_datepoint, $month, $day) |
|
340
|
6 |
|
->setTime($hour, $minute, $second); |
|
341
|
|
|
|
|
342
|
6 |
|
return Period::after($startDate, new DateInterval('PT1S')); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
6 |
|
return Datepoint::create($year_or_datepoint)->getSecond(); |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Creates new instance for a specific datepoint. |
|
350
|
|
|
* |
|
351
|
|
|
* DEPRECATION WARNING! This function will be removed in the next major point release |
|
352
|
|
|
* |
|
353
|
|
|
* @deprecated deprecated since version 4.2 |
|
354
|
|
|
* @see Period::__construct |
|
355
|
|
|
* |
|
356
|
|
|
* @param mixed $year_or_datepoint a year as an int or a datepoint |
|
357
|
|
|
*/ |
|
358
|
|
|
function instant( |
|
359
|
|
|
$year_or_datepoint, |
|
360
|
|
|
int $month = 1, |
|
361
|
|
|
int $day = 1, |
|
362
|
|
|
int $hour = 0, |
|
363
|
|
|
int $minute = 0, |
|
364
|
|
|
int $second = 0, |
|
365
|
|
|
int $microsecond = 0 |
|
366
|
|
|
): Period { |
|
367
|
12 |
|
if (is_int($year_or_datepoint)) { |
|
368
|
6 |
|
$year_or_datepoint = (new DateTimeImmutable()) |
|
369
|
6 |
|
->setDate($year_or_datepoint, $month, $day) |
|
370
|
6 |
|
->setTime($hour, $minute, $second, $microsecond); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
12 |
|
return new Period($year_or_datepoint, $year_or_datepoint); |
|
374
|
|
|
} |
|
375
|
|
|
|