-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateTime.php
497 lines (443 loc) · 11.6 KB
/
DateTime.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
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\Utils;
use DateTimeZone;
use DateInterval;
use Exception;
/**
* DateTime
*/
class DateTime
{
const DEFAULT_DATE_FORMAT = 'Y-m-d';
const DEFAULT_TIME_FORMAT = 'H:i';
const ISO8601_FORMAT = DATE_ATOM;
const RFC3339_FORMAT = DATE_RFC3339;
const ISO8601ZULU_FORMAT = 'Y-m-d\TH:i:s\Z';
const ICALENDAR_EVENT_UTC = 'Ymd\THis\Z';
const ICALENDAR_EVENT = 'Ymd\THis';
/**
* Time zone
*
* @var object
*/
private static $timeZone;
/**
* DateTime object
*
* @var \DateTime
*/
private static $dateTime;
/**
* Date format
*
* @var string|null
*/
private static $dateFormat = null;
/**
* Time formats list
*
* @var string|null
*/
private static $timeFormat = null;
/**
* Set date format
*
* @param string|null $format
* @return void
*/
public static function setDateFormat(?string $format = null): void
{
Self::$dateFormat = $format;
}
/**
* Set time formats
*
* @param string|null $format
* @return void
*/
public static function setTimeFormat(?string $format = null): void
{
Self::$timeFormat = $format;
}
/**
* Set date time obj
*
* @param string|null $date
* @param string|null $format
* @return void
*/
public static function setDateTime(?string $date = null, ?string $format = null): void
{
Self::$dateTime = Self::create($date,$format);
}
/**
* Create DateTime obj
*
* @param string|null $date
* @param string|null $format
* @return \DateTime
*/
public static function create(?string $date = null, ?string $format = null): object
{
if (empty($date) == true || $date == 'now') {
$dateTime = new \DateTime('now',Self::getTimeZone());
$dateTime->format($format ?? Self::getDateFormat());
return $dateTime;
}
return \DateTime::createFromFormat(
$format ?? Self::getDateFormat(),
$date,
Self::getTimeZone()
);
}
/**
* Get DateTime
*
* @return \DateTime
*/
public static function getDateTime(): object
{
if (empty(Self::$dateTime) == true) {
Self::setDateTime();
}
return Self::$dateTime;
}
/**
* Get timestamp
*
* @return int
*/
public static function getTimestamp(): int
{
return Self::getDateTime()->getTimestamp();
}
/**
* Get current timestamp
*
* @return int
*/
public static function getCurrentTimestamp(): int
{
return Self::create()->getTimestamp();
}
/**
* Create form timestamp
*
* @param integer $timestamp
* @param string|null $format
* @return object
*/
public static function createFromTimestamp(int $timestamp, ?string $format = null): object
{
$date = Self::create(null,$format);
$date->setTimestamp($timestamp);
return $date;
}
/**
* Comvert date time to timestamp
*
* @param string|null $date
* @param string|null $format
* @return integer
*/
public static function toTimestamp(?string $date = null, ?string $format = null): int
{
return Self::create($date,$format)->getTimestamp();
}
/**
* Get date format
*
* @return string
*/
public static function getDateFormat(): string
{
return Self::$dateFormat ?? \constant('CURRENT_DATE_FORMAT') ?? Self::DEFAULT_DATE_FORMAT;
}
/**
* Get time zone list
*
* @return array|false
*/
public static function getTimeZonesList()
{
return \timezone_identifiers_list();
}
/**
* Get location
*
* @return mixed
*/
public static function getLocation()
{
return Self::getTimeZone()->getLocation();
}
/**
* Get time zone offset
*
* @param DateTime|null $dateTime
* @return string
*/
public static function getTimeZoneOffset($dateTime = null): string
{
return Self::getTimeZone()->getOffset($dateTime ?? Self::$dateTime);
}
/**
* Get time zone
*
* @return string
*/
public static function getTimeZoneName(): string
{
return Self::getTimeZone()->getName();
}
/**
* Get time zone
*
* @return DateTimeZone|null
*/
public static function getTimeZone(): ?object
{
if (empty(Self::$timeZone) == true) {
Self::setTimeZone(\date_default_timezone_get());
}
return Self::$timeZone;
}
/**
* Return true if time zone is vlaid
*
* @param string|null $name
* @return boolean
*/
public static function isValidTimeZone(?string $name): bool
{
return (empty($name) == true) ? false : \in_array($name,\timezone_identifiers_list());
}
/**
* Set time zone
*
* @param string|null $name
* @return void
*/
public static function setTimeZone(?string $name): void
{
$name = (Self::isValidTimeZone($name) == false || empty($name) == true) ? \date_default_timezone_get() : $name;
Self::$timeZone = new DateTimeZone($name);
}
/**
* Return formated timestsamp with current date and time format
*
* @param integer|null $timestamp
* @param string|null $format
* @return string|null
*/
public static function dateTimeFormat(?int $timestamp, ?string $format = null): ?string
{
if ($timestamp === null) {
return null;
}
$format = $format ?? Self::getDateFormat() . ' ' . Self::getTimeFormat();
return Self::setTimestamp($timestamp)->format($format);
}
/**
* Return formated time
*
* @param integer|null $timestamp
* @param string $format
* @return string|null
*/
public static function timeFormat(?int $timestamp, ?string $format = null): ?string
{
return (\is_numeric($timestamp) == false) ? $timestamp :
Self::setTimestamp($timestamp)->format($format ?? Self::getTimeFormat());
}
/**
* Return formated date
*
* @param integer|null $timestamp
* @param string|null $format
* @return string|null
*/
public static function dateFormat(?int $timestamp, ?string $format = null): ?string
{
return (\is_numeric($timestamp) == false) ? $timestamp :
Self::setTimestamp((integer)$timestamp)->format($format ?? Self::getDateFormat());
}
/**
* Get time format
*
* @return string
*/
public static function getTimeFormat(): string
{
return Self::$timeFormat ?? \constant('CURRENT_TIME_FORMAT') ?? Self::DEFAULT_TIME_FORMAT;
}
/**
* Get interval details
*
* @param string $intervalText
* @return array
*/
public static function getInterval(string $intervalText): array
{
return (new TimeInterval($intervalText))->toArray();
}
/**
* Modify date time
*
* @param string|null $dateText
* @return \DateTime
*/
public static function modify(?string $dateText): object
{
Self::$dateTime = Self::getDateTime()->modify($dateText);
return Self::$dateTime;
}
/**
* Add interval
*
* @param string $dateInterval
* @return \DateTime
*/
public static function addInterval(string $dateInterval): object
{
try {
$interval = DateInterval::createFromDateString($dateInterval);
} catch (\Exception $e) {
$interval = new DateInterval($dateInterval);
}
$interval = ($interval === false) ? new DateInterval($dateInterval) : $interval;
return Self::getDateTime()->add($interval);
}
/**
* Sub interval
*
* @param string $dateInterval
* @return \DateTime
*/
public static function subInterval(string $dateInterval): object
{
try {
$interval = DateInterval::createFromDateString($dateInterval);
$interval = ($interval === false) ? new DateInterval($dateInterval) : $interval;
} catch(Exception $e) {
$interval = new DateInterval($dateInterval);
}
return Self::getDateTime()->sub($interval);
}
/**
* Set timestamp
*
* @param integer $unixTimestamp
* @return \DateTime
*/
public static function setTimestamp(int $unixTimestamp): object
{
Self::$dateTime = Self::getDateTime()->setTimestamp($unixTimestamp);
return Self::$dateTime;
}
/**
* Get curent year
*
* @return string
*/
public static function getYear(): string
{
return \date('Y',Self::toTimestamp());
}
/**
* Get current year
*
* @return string
*/
public static function getCurrentYear(): string
{
return \date('Y');
}
/**
* Get last day of month
*
* @param integer|string $month
* @return integer
*/
public static function getLastDay($month): string
{
$date = Self::getYear() . '-' . $month . '-01';
return \date('t',Self::toTimestamp($date,'Y-m-d'));
}
/**
* Get current month
*
* @return string
*/
public static function getMonth(): string
{
return \date('n',Self::getTimestamp());
}
/**
* Return current day
*
* @return string
*/
public static function getDay(): string
{
return \date('j',Self::getTimestamp());
}
/**
* Return current hour
*
* @return string
*/
public static function getHour(): string
{
return \date('G',Self::getTimestamp());
}
/**
* Get current minutes
*
* @return string
*/
public static function getMinutes(): string
{
return \intval(\date('i',Self::getTimestamp()));
}
/**
* Convert current date time to string.
*
* @param string|null $format
* @return string
*/
public static function toString(?string $format = null): string
{
return Self::getDateTime()->format($format ?? Self::getDateFormat());
}
/**
* Get current date
*
* @param string|null $format
* @return \DateTime
*/
public static function today(?string $format = null): object
{
return Self::create('now',$format);
}
/**
* Convert date string to other format
*
* @param string $date
* @param string|null $format
* @return string|false
*/
public static function convert($date, ?string $format = null)
{
return (\strtotime($date) === false) ? false :
Self::create($date)->format($format ?? Self::getDateFormat());
}
}