This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
IntlDateTrait.php
676 lines (559 loc) · 18 KB
/
IntlDateTrait.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
<?php
/**
* IntlDate is a trait for easy conversion date and time between multiple calendars.
*
* @category Traits
*
* @author meysampg <[email protected]>
* @license https://github.com/meysampg/intldate/blob/master/LICENSE MIT
*
* @version 1.1
*
* @link https://github.com/meysampg/intldate
*/
namespace meysampg\intldate;
use Exception;
use IntlCalendar;
use IntlDateFormatter;
trait IntlDateTrait
{
private $_fromLocale;
private $_toLocale;
private $_fromCalendar;
private $_toCalendar;
private $_intlDateFormatter;
private $_intlCalendar;
public static $CAL_PERSIAN = 'persian';
public static $CAL_JAPANESE = 'japanese';
public static $CAL_BUDDHIST = 'buddhist';
public static $CAL_CHINESE = 'chinese';
public static $CAL_INDIAN = 'indian';
public static $CAL_ISLAMIC = 'islamic';
public static $CAL_HEBREW = 'hebrew';
public static $CAL_COPTIC = 'coptic';
public static $CAL_ETHIOPIC = 'ethiopic';
public static $CAL_GREGORIAN = '';
/**
* Return final date and time on supplied format.
*
* @param string $pattern pattern of datetime based on ICU standards
*
* @return string
*
* @since 1.0.0
*/
public function asDateTime($pattern = 'yyyy/MM/dd, HH:mm:ss')
{
$this->setFinalPattern($this->parsePattern($pattern));
return $this->getIntlDateFormatter()->format($this->getIntlCalendar());
}
/**
* Return final time as a timestamp.
*
* @return int
*
* @since 1.0.0
*/
public function asTimestamp()
{
return $this->getIntlCalendar()->toDateTime()->format('U');
}
/**
* Get datetime as a timestamp.
*
* @param int $timestamp timestamp on origin
*
* @return static
*
* @since 1.0.3
*/
public function fromTimestamp($timestamp, $timezone = 'UTC')
{
// [TODO] use DateTime object for parse timestamp
$oldTz = date_default_timezone_get();
date_default_timezone_set($timezone);
$timestamp = mktime(date('H', $timestamp), date('i', $timestamp), date('s', $timestamp), date('n', $timestamp), date('j', $timestamp), date('Y', $timestamp));
date_default_timezone_set('UTC');
$utcDT = gmdate('c', $timestamp);
$dateArray = getdate(strtotime(gmdate('c', $timestamp)));
date_default_timezone_set($oldTz);
unset($dateArray[0]);
$this->from($dateArray, 'en_US', null, $timezone);
return $this;
}
/**
* Get information of datetime on origin.
*
* @param array $datetime Array contains datetime information. Its elements
* are [year, month, day, hour, minute, second].
* @param string $locale locale for showing datetime on it (e.g. `en_US` or
* `fa_IR`, 'es_US', ...)
* @param string $calendar Calendar on origin
* @param string $timezone Timezone on origin
*
* @return static
*
* @since 1.0.0
*/
public function from(
$datetime = [],
$locale = 'en_US',
$calendar = null,
$timezone = 'UTC'
) {
$datetime = $this->parseDateTime($datetime);
$calendar = $calendar ?: self::$CAL_GREGORIAN;
$this->setIntlCalendar()
->setFromCalendar($calendar)->setFromLocale($locale)
->setOriginCalendar($this->getFromLocaleAndCalendar())
->setOriginTimeZone($timezone)
->setOriginDate($datetime);
return $this;
}
/**
* Convert datetime to a desired calendar.
*
* @param string $locale locale for showing new datetime
* @param mixed $calendar calendar system for showing new datetime
* @param string $timezone timezone of destination
*
* @return static
*/
public function to(
$locale = 'en_US',
$calendar = null,
$timezone = 'UTC'
) {
$calendar = $calendar !== null ? $calendar : self::$CAL_PERSIAN;
$this->setIntlDateFormatter()
->setToCalendar($calendar)->setToLocale($locale)
->setFinalTimeZone($timezone)
->setFinalCalendar($this->getToLocaleAndCalendar());
if ($calendar == self::$CAL_GREGORIAN) {
$this->setFinalCalendarType(IntlDateFormatter::GREGORIAN);
} else {
$this->setFinalCalendarType(IntlDateFormatter::TRADITIONAL);
}
return $this;
}
public function fromPersian($datetime, $locale = 'en_US', $timezone = 'UTC')
{
$this->from($datetime, $locale, self::$CAL_PERSIAN, $timezone);
return $this;
}
public function toPersian($locale = 'fa', $timezone = 'UTC')
{
$this->to($locale, self::$CAL_PERSIAN, $timezone);
return $this;
}
public function fromJapanese($datetime, $locale = 'en_US', $timezone = 'UTC')
{
$this->from($datetime, $locale, self::$CAL_JAPANESE, $timezone);
return $this;
}
public function toJapanese($locale = 'jp', $timezone = 'UTC')
{
$this->to($locale, self::$CAL_JAPANESE, $timezone);
return $this;
}
public function fromBuddhist($datetime, $locale = 'en_US', $timezone = 'UTC')
{
$this->from($datetime, $locale, self::$CAL_BUDDHIST, $timezone);
return $this;
}
public function toBuddhist($locale = 'th', $timezone = 'UTC')
{
$this->to($locale, self::$CAL_BUDDHIST, $timezone);
return $this;
}
public function fromChinese($datetime, $locale = 'en_US', $timezone = 'UTC')
{
$this->from($datetime, $locale, self::$CAL_CHINESE, $timezone);
return $this;
}
public function toChinese($locale = 'ch', $timezone = 'UTC')
{
$this->to($locale, self::$CAL_CHINESE, $timezone);
return $this;
}
public function fromIndian($datetime, $locale = 'en_US', $timezone = 'UTC')
{
$this->from($datetime, $locale, self::$CAL_INDIAN, $timezone);
return $this;
}
public function toIndian($locale = 'hi', $timezone = 'UTC')
{
$this->to($locale, self::$CAL_INDIAN, $timezone = 'UTC', $timezone);
return $this;
}
public function fromIslamic($datetime, $locale = 'en_US', $timezone = 'UTC')
{
$this->from($datetime, $locale, self::$CAL_ISLAMIC, $timezone);
return $this;
}
public function toIslamic($locale = 'ar', $timezone = 'UTC')
{
$this->to($locale, self::$CAL_ISLAMIC, $timezone);
return $this;
}
public function fromHebrew($datetime, $locale = 'en_US', $timezone = 'UTC')
{
$this->from($datetime, $locale, self::$CAL_HEBREW, $timezone);
return $this;
}
public function toHebrew($locale = 'he', $timezone = 'UTC')
{
$this->to($locale, self::$CAL_HEBREW, $timezone);
return $this;
}
public function fromCoptic($datetime, $locale = 'en_US', $timezone = 'UTC')
{
$this->from($datetime, $locale, self::$CAL_COPTIC, $timezone);
return $this;
}
public function toCoptic($locale = 'en_US', $timezone = 'UTC')
{
$this->to($locale, self::$CAL_COPTIC, $timezone);
return $this;
}
public function fromEthiopic($datetime, $locale = 'en_US', $timezone = 'UTC')
{
$this->from($datetime, $locale, self::$CAL_ETHIOPIC, $timezone);
return $this;
}
public function toEthiopic($locale = 'am', $timezone = 'UTC')
{
$this->to($locale, self::$CAL_ETHIOPIC, $timezone);
return $this;
}
public function fromGregorian($datetime, $locale = 'en_US', $timezone = 'UTC')
{
$this->from($datetime, $locale, self::$CAL_GREGORIAN, $timezone);
return $this;
}
public function toGregorian($locale = 'en_US', $timezone = 'UTC')
{
$this->to($locale, self::$CAL_GREGORIAN, $timezone);
return $this;
}
public function setOriginDate($datetimeArray)
{
$this->getIntlCalendar()->set($datetimeArray[0], $datetimeArray[1], $datetimeArray[2], $datetimeArray[3], $datetimeArray[4], $datetimeArray[5]);
return $this;
}
public function getFinalDate()
{
return $this->getIntlDateFormatter()->formatter($this->getIntlCalendar());
}
public function setFromLocale($locale)
{
$this->_fromLocale = trim($locale);
return $this;
}
public function getFromLocale()
{
return $this->_fromLocale;
}
public function setFromCalendar($calendar)
{
$this->_fromCalendar = '@calendar='.trim($calendar);
return $this;
}
public function getFromCalendar()
{
return $this->_fromCalendar;
}
public function setToLocale($locale)
{
$this->_toLocale = trim($locale);
return $this;
}
public function getToLocale()
{
return $this->_toLocale;
}
public function setToCalendar($calendar)
{
$this->_toCalendar = '@calendar='.trim($calendar);
return $this;
}
public function getToCalendar()
{
return $this->_toCalendar;
}
public function getFromLocaleAndCalendar()
{
return $this->getFromLocale().$this->getFromCalendar();
}
public function getToLocaleAndCalendar()
{
return $this->getToLocale().$this->getToCalendar();
}
public function setOriginTimeZone($timezone)
{
$this->getIntlCalendar()->setTimeZone($timezone);
return $this;
}
public function getOriginTimeZone()
{
return $this->getIntlCalendar()->getTimeZone();
}
public function setFinalTimeZone($timezone)
{
$this->setIntlDateFormatter(
$this->getToLocaleAndCalendar(),
$this->getFinalDateType(),
$this->getFinalTimeType(),
$timezone,
$this->getFinalCalendarType(),
$this->getFinalPattern()
);
return $this;
}
public function getFinalTimeZone()
{
return $this->getIntlDateFormatter()->getTimeZone();
}
public function setOriginCalendar($locale)
{
$this->setIntlCalendar(
$this->getOriginTimeZone(),
$locale
);
return $this;
}
public function getOriginCalendar()
{
return $this->getIntlCalendar()->getLocale();
}
public function setFinalCalendar($locale)
{
$this->setIntlDateFormatter(
$locale,
$this->getFinalDateType(),
$this->getFinalTimeType(),
$this->getFinalTimeZone(),
$this->getFinalCalendarType(),
$this->getFinalPattern(),
true
);
return $this;
}
public function getFinalCalendar()
{
return $this->getIntlDateFormatter()->getLocale();
}
public function setFinalDateType($datetype)
{
$this->setIntlDateFormatter(
$this->getToLocaleAndCalendar(),
$datetype,
$this->getFinalTimeType(),
$this->getFinalTimeZone(),
$this->getFinalCalendarType(),
$this->getFinalPattern(),
true
);
return $this;
}
public function getFinalDateType()
{
$this->getIntlDateFormatter()->getDateType();
}
public function setFinalTimeType($timetype)
{
$this->setIntlDateFormatter(
$this->getToLocaleAndCalendar(),
$this->getFinalDateType(),
$timetype,
$this->getFinalTimeZone(),
$this->getFinalCalendarType(),
$this->getFinalPattern(),
true
);
return $this;
}
public function getFinalTimeType()
{
return $this->getIntlDateFormatter()->getTimeType();
}
public function setFinalCalendarType($calendarType)
{
$this->setIntlDateFormatter(
$this->getToLocaleAndCalendar(),
$this->getFinalDateType(),
$this->getFinalTimeType(),
$this->getFinalTimeZone(),
$calendarType,
$this->getFinalPattern(),
true
);
return $this;
}
public function getFinalCalendarType()
{
return $this->getIntlDateFormatter()->getCalendar();
}
public function setFinalPattern($pattern)
{
$this->getIntlDateFormatter()->setPattern($pattern);
return $this;
}
public function getFinalPattern()
{
return $this->getIntlDateFormatter()->getPattern();
}
// [TODO]
public function parsePattern($pattern)
{
/*
* Implement a function to parse both ICU patterns and php date
* function patterns and return a pattern that is compatible on
* ICU format. The php pattern must start with php keyword, for
* example `php:Y-F-d, H:i:s` is a php pattern.
*/
return $pattern;
}
/**
* This method gets a datetime string as the input and converts it
* into a standard array.
* Example: '2016/01/22 11:43:24'
* to an array like:
* [
* 0 => 2016, // Year
* 1 => 0, // Month. IntlCalendar use 0 for first month and so on.
* 2 => 22, // Day
* 3 => 11, // Hour
* 4 => 43, // Minute
* 5 => 24, // Seconds
*]
* Returns False if invalid month, day, hour, minute, or second is given.
*/
public function guessDateTime($timestring)
{
$date_time = explode(' ', $timestring);
$date = explode('/', $date_time[0]);
$time = explode(':', $date_time[1]);
$year = (int) $date[0];
$month = $date[1];
$day = (int) $date[2];
$hour = (int) $time[0];
$minute = (int) $time[1];
$second = (int) $time[2];
if ($day > 31 || $day < 1) {
return false;
} elseif ($hour > 24 || $hour < 0) {
return false;
} elseif ($minute > 60 || $minute < 0) {
return false;
} elseif ($second > 60 || $second < 0) {
return false;
}
switch ($month) {
case '01':
$month = 0;
break;
case '02':
$month = 1;
break;
case '03':
$month = 2;
break;
case '04':
$month = 3;
break;
case '05':
$month = 4;
break;
case '06':
$month = 5;
break;
case '07':
$month = 6;
break;
case '08':
$month = 7;
break;
case '09':
$month = 8;
break;
case '10':
$month = 9;
break;
case '11':
$month = 10;
break;
case '12':
$month = 11;
break;
default:
return false;
break;
}
return [$year, $month, $day, $hour, $minute, $second];
}
/**
* Parse DateTime information array to be in correct format.
*
* @param array $datetimeArray array contains information of DateTime in
* `year, month, day, hour, minute, day` order. This parameter can be a
* either associative or non-associative array. For the former, keys must
* be compitiable with http://php.net/manual/en/function.getdate.php. For
* missing pieces of information, a corresponded part from 1970/1/Jan., 00:00:00
* will be replaced.
*
* @throws Exception
*
* @return array An `IntlDateFormatter` compatible array.
*/
private function parseDateTime($datetimeArray)
{
$finalDatetimeArray = [];
if (!is_array($datetimeArray)) {
throw new Exception('DateTime information must be an array in [year, month, day, hours, minutes, seconds] format.');
}
$finalDatetimeArray[0] = isset($datetimeArray[0]) ? (int) $datetimeArray[0] : (isset($datetimeArray['year']) ? (int) $datetimeArray['year'] : 1970);
$finalDatetimeArray[1] = isset($datetimeArray[1]) ? (int) $datetimeArray[1] - 1 : (isset($datetimeArray['mon']) ? (int) $datetimeArray['mon'] - 1 : 0);
$finalDatetimeArray[2] = isset($datetimeArray[2]) ? (int) $datetimeArray[2] : (isset($datetimeArray['mday']) ? (int) $datetimeArray['mday'] : 1);
$finalDatetimeArray[3] = isset($datetimeArray[3]) ? (int) $datetimeArray[3] : (isset($datetimeArray['hours']) ? (int) $datetimeArray['hours'] : 0);
$finalDatetimeArray[4] = isset($datetimeArray[4]) ? (int) $datetimeArray[4] : (isset($datetimeArray['minutes']) ? (int) $datetimeArray['minutes'] : 0);
$finalDatetimeArray[5] = isset($datetimeArray[5]) ? (int) $datetimeArray[5] : (isset($datetimeArray['seconds']) ? (int) $datetimeArray['seconds'] : 0);
return $finalDatetimeArray;
}
public function setIntlDateFormatter(
$locale = 'en_US',
$datetype = IntlDateFormatter::FULL,
$timetype = IntlDateFormatter::FULL,
$timezone = 'UTC',
$calendar = IntlDateFormatter::GREGORIAN,
$pattern = 'yyyy/MM/dd HH:mm:ss'
) {
$this->_intlDateFormatter = new IntlDateFormatter(
$locale, // string $locale
$datetype, // int $datetype
$timetype, // int $timetype
$timezone, // mixed $timezone
$calendar, // mixed $calendar
$pattern // string $pattern
);
return $this;
}
public function getIntlDateFormatter()
{
return $this->_intlDateFormatter;
}
public function setIntlCalendar(
$timezone = 'UTC',
$locale = 'en_US@calendar=gregorian'
) {
$this->_intlCalendar = IntlCalendar::createInstance(
$timezone,
$locale
);
return $this;
}
public function getIntlCalendar()
{
return $this->_intlCalendar;
}
}