-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDate.php
executable file
·542 lines (488 loc) · 13.9 KB
/
Date.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
/**
* Copyright 2021 Jeremy Presutti <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare(strict_types=1);
namespace Feast;
use DateTime;
use DateTimeZone;
use Exception;
use Feast\Exception\InvalidDateException;
/**
* Manages Dates from different formats to different formats
* This class is an alternative to \DateTime
*/
class Date
{
final public const FORMAT_YMD_WITH_SLASHES = 'Y/m/d';
final public const FORMAT_YMD_HMS_WITH_SLASHES = 'Y/m/d H:i:s';
final public const FORMAT_YMD_HM_WITH_SLASHES = 'Y/m/d H:i';
final public const FORMAT_YMD_WITH_DASHES = 'Y-m-d';
final public const FORMAT_YMD_HMS_WITH_DASHES = 'Y-m-d H:i:s';
final public const FORMAT_YMD_HM_WITH_DASHES = 'Y-m-d H:i';
final public const FORMAT_MDY_WITH_SLASHES = 'm/d/Y';
final public const FORMAT_MDY_HMS_WITH_SLASHES = 'm/d/Y H:i:s';
final public const FORMAT_MDY_HM_WITH_SLASHES = 'm/d/Y H:i';
final public const FORMAT_MDY_WITH_DASHES = 'm-d-Y';
final public const FORMAT_MDY_HMS_WITH_DASHES = 'm-d-Y H:i:s';
final public const FORMAT_MDY_HM_WITH_DASHES = 'm-d-y H:i';
final public const FORMAT_MDY_TEXT_FORMAT_WITHOUT_SUFFIX = 'F j, Y';
final public const FORMAT_MDY_TEXT_FORMAT_WITH_SUFFIX = 'F jS, Y';
final public const FORMAT_HMS = 'H:i:s';
final public const FORMAT_HM = 'H:i';
final public const FORMAT_UNIXTIMESTAMP = 'U';
final public const ATOM = "Y-m-d\TH:i:sP";
final public const COOKIE = "l, d-M-Y H:i:s T";
final public const ISO8601 = "Y-m-d\TH:i:sO";
final public const RFC822 = "D, d M y H:i:s O";
final public const RFC850 = "l, d-M-y H:i:s T";
final public const RFC1036 = "D, d M y H:i:s O";
final public const RFC1123 = "D, d M Y H:i:s O";
final public const RFC2822 = "D, d M Y H:i:s O";
final public const RFC3339 = "Y-m-d\TH:i:sP";
final public const RFC3339_EXTENDED = "Y-m-d\TH:i:s.vP";
final public const RSS = "D, d M Y H:i:s O";
final public const W3C = "Y-m-d\TH:i:sP";
private int $timestamp;
protected ?string $timezone = null;
protected string $serverTimezone;
/**
* Create Date object from Unix Timestamp.
*
* @param int $timestamp
* @param string|null $timezone
* @return Date
* @throws InvalidDateException
*/
public static function createFromTimestamp(int $timestamp, ?string $timezone = null): self
{
$serverTimezone = date_default_timezone_get();
if ($timezone) {
date_default_timezone_set($timezone);
}
$params = [
date('Y', $timestamp), date('m', $timestamp), date('d', $timestamp), date('H', $timestamp),
date('i', $timestamp),
date('s', $timestamp),
$timezone
];
date_default_timezone_set($serverTimezone);
return new self(...$params);
}
/**
* Create Date object from now.
*
* @param string|null $timezone
* @return self
* @throws InvalidDateException
*/
public static function createFromNow(?string $timezone = null): self
{
return self::createFromTimestamp(time(), $timezone);
}
/**
* Create Date object from date string.
*
* Example: 2020-01-01; 2021-03-26 14:34:45
*
* @param string $date
* @param string|null $timezone
* @return self
* @throws InvalidDateException
*/
public static function createFromString(string $date, ?string $timezone = null): self
{
// Timestamp based
$serverTimezone = date_default_timezone_get();
if ($timezone) {
date_default_timezone_set($timezone);
}
if (str_contains($date, '-') && str_contains($date, '/')) {
throw new InvalidDateException('Invalid date string - ' . $date);
}
if (strtotime($date) === false) {
throw new InvalidDateException('Invalid date string - ' . $date);
}
$date = date('Y-m-d H:i:s', strtotime($date . ' ' . ($timezone ?: '')));
$params = [
substr($date, 0, 4), substr($date, 5, 2), substr($date, 8, 2), substr($date, 11, 2),
substr($date, 14, 2), substr($date, 17, 2), $timezone];
date_default_timezone_set($serverTimezone);
return new self(...$params);
}
/**
* Create a Date object from a specific format string.
*
* This function uses PHP's built in DateTime as an intermediary
* and follows all the rules of that class' constructor.
*
* @param string $format
* @param string $dateString
* @param string|null $timezone
* @return self
* @throws InvalidDateException
*/
public static function createFromFormat(string $format, string $dateString, ?string $timezone = null): self
{
$timezoneData = isset($timezone) ? new DateTimeZone($timezone) : null;
$date = DateTime::createFromFormat($format, $dateString, $timezoneData);
return self::createFromTimestamp($date->getTimestamp(), $timezone);
}
/**
* @throws InvalidDateException
*/
public function __construct(
string|int $year,
string|int $month,
string|int $day,
string|int $hour = 0,
string|int $minute = 0,
string|int $second = 0,
string $timezone = null
) {
if (!ctype_digit((string)$year) ||
!ctype_digit((string)$month) ||
!ctype_digit((string)$day) ||
!ctype_digit((string)$hour) ||
!ctype_digit((string)$minute) ||
!ctype_digit((string)$second)) {
throw new InvalidDateException('Invalid date');
}
$year = (int)$year;
$month = (int)$month;
$day = (int)$day;
$hour = (int)$hour;
$minute = (int)$minute;
$second = (int)$second;
$this->throwIfInvalidDate($hour, $minute, $second, $month, $day, $year);
$this->serverTimezone = date_default_timezone_get();
if ($timezone) {
$this->timezone = $timezone;
date_default_timezone_set($timezone);
}
$this->timestamp = mktime($hour, $minute, $second, $month, $day, $year);
date_default_timezone_set($this->serverTimezone);
}
/**
* Set timezone for date object.
*
* @param string $timezone
*/
public function setTimezone(string $timezone): void
{
$this->timezone = $timezone;
}
/**
* Get Unix Timestamp.
*
* @return int
*/
public function getTimestamp(): int
{
return $this->timestamp;
}
/**
* Get year.
*
* @return string
*/
public function getYear(): string
{
return $this->getFormattedDate('Y');
}
/**
* Get Month (2 digit numeric format).
*
* @return string
*/
public function getMonth(): string
{
return $this->getFormattedDate('m');
}
/**
* Get day of year.
*
* @return string
*/
public function getDay(): string
{
return $this->getFormattedDate('d');
}
/**
* Get hour.
*
* @return string
*/
public function getHour(): string
{
return $this->getFormattedDate('H');
}
/**
* Get minute.
*
* @return string
*/
public function getMinute(): string
{
return $this->getFormattedDate('i');
}
/**
* Get second.
*
* @return string
*/
public function getSecond(): string
{
return $this->getFormattedDate('s');
}
/**
* Get day of the week (0 = Sunday, 6 = Saturday).
*
* @return string
*/
public function getDayOfWeek(): string
{
return $this->getFormattedDate('w');
}
/**
* Get day of the year (0-365).
*
* @return string
*/
public function getDayOfYear(): string
{
return $this->getFormattedDate('z');
}
public function isDaylightSavingsTime(): bool
{
return $this->getFormattedDate('I') === '1';
}
public function isLeapYear(): bool
{
return $this->getFormattedDate('L') === '1';
}
/**
* Set the year.
*
* @param int $year
* @return self
*/
public function setYear(int $year): self
{
$this->timestamp = mktime(
(int)$this->getHour(),
(int)$this->getMinute(),
(int)$this->getSecond(),
(int)$this->getMonth(),
(int)$this->getDay(),
$year
);
return $this;
}
/**
* Set the month.
*
* @param int $month
* @return self
*/
public function setMonth(int $month): self
{
$this->timestamp = mktime(
(int)$this->getHour(),
(int)$this->getMinute(),
(int)$this->getSecond(),
$month,
(int)$this->getDay(),
(int)$this->getYear()
);
return $this;
}
/**
* Set the day.
*
* @param int $day
* @return self
*/
public function setDay(int $day): self
{
$this->timestamp = mktime(
(int)$this->getHour(),
(int)$this->getMinute(),
(int)$this->getSecond(),
(int)$this->getMonth(),
$day,
(int)$this->getYear()
);
return $this;
}
/**
* Set the hour.
*
* @param int $hour
* @return self
*/
public function setHour(int $hour): self
{
$this->timestamp = mktime(
$hour,
(int)$this->getMinute(),
(int)$this->getSecond(),
(int)$this->getMonth(),
(int)$this->getDay(),
(int)$this->getYear()
);
return $this;
}
/**
* Set the minute.
*
* @param int $minute
* @return self
*/
public function setMinute(int $minute): self
{
$this->timestamp = mktime(
(int)$this->getHour(),
$minute,
(int)$this->getSecond(),
(int)$this->getMonth(),
(int)$this->getDay(),
(int)$this->getYear()
);
return $this;
}
/**
* Set the second.
*
* @param int $second
* @return self
*/
public function setSecond(int $second): self
{
$this->timestamp = mktime(
(int)$this->getHour(),
(int)$this->getMinute(),
$second,
(int)$this->getMonth(),
(int)$this->getDay(),
(int)$this->getYear()
);
return $this;
}
/**
* Get date string for the chosen format.
*
* @param string $format
* @return string
*/
public function getFormattedDate(string $format = 'Y-m-d H:i:s'): string
{
if ($this->timezone !== null) {
date_default_timezone_set($this->timezone);
}
$return = date($format, $this->timestamp);
date_default_timezone_set($this->serverTimezone);
return $return;
}
public function __toString(): string
{
return $this->getFormattedDate();
}
/**
* Modify date by a text based format.
*
* @param string $amount
* @return $this
* @throws InvalidDateException
*/
public function modify(string $amount): Date
{
$modification = strtotime($amount, $this->timestamp);
if ($modification === false) {
throw new InvalidDateException('Invalid date modification');
}
$this->timestamp = $modification;
return $this;
}
/**
* Get as PHP DateTime object.
*
* @return DateTime
* @throws Exception
*/
public function getAsDateTime(): DateTime
{
$timezone = $this->timezone ? new DateTimeZone($this->timezone) : null;
return new DateTime($this->getFormattedDate(self::ISO8601), $timezone);
}
/**
* Check if this Date is greater than or equal to a passed in Date.
*
* @param Date $date
* @return bool
*/
public function greaterThanEqual(Date $date): bool
{
return $this->timestamp >= $date->timestamp;
}
/**
* Check if this Date is greater than to a passed in Date.
*
* @param Date $date
* @return bool
*/
public function greaterThan(Date $date): bool
{
return $this->timestamp > $date->timestamp;
}
/**
* Check if this Date is less than or equal to a passed in Date.
*
* @param Date $date
* @return bool
*/
public function lessThanEqual(Date $date): bool
{
return $this->timestamp <= $date->timestamp;
}
/**
* Check if this Date is less than to a passed in Date.
*
* @param Date $date
* @return bool
*/
public function lessThan(Date $date): bool
{
return $this->timestamp < $date->timestamp;
}
/**
* @throws InvalidDateException
*/
protected function throwIfInvalidDate(int $hour, int $minute, int $second, int $month, int $day, int $year): void
{
if (checkdate($month, $day, $year) === false) {
throw new InvalidDateException('Invalid date');
}
if ($hour > 23 || $hour < 0) {
throw new InvalidDateException('Invalid date');
}
if ($minute > 59 || $minute < 0) {
throw new InvalidDateException('Invalid date');
}
if ($second > 59 || $second < 0) {
throw new InvalidDateException('Invalid date');
}
}
}