-
Notifications
You must be signed in to change notification settings - Fork 5
/
carbon.php
2276 lines (2276 loc) · 63.3 KB
/
carbon.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <[email protected]>
*https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Carbon.php
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Carbon;
use Closure;
use DateTime;
use DateTimeZone;
use DatePeriod;
use InvalidArgumentException;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\Loader\ArrayLoader;
/**
* A simple API extension for DateTime
*
* @property integer $year
* @property integer $yearIso
* @property integer $month
* @property integer $day
* @property integer $hour
* @property integer $minute
* @property integer $second
* @property integer $timestamp seconds since the Unix Epoch
* @property DateTimeZone $timezone the current timezone
* @property DateTimeZone $tz alias of timezone
* @property-read integer $micro
* @property-read integer $dayOfWeek 0 (for Sunday) through 6 (for Saturday)
* @property-read integer $dayOfYear 0 through 365
* @property-read integer $weekOfMonth 1 through 5
* @property-read integer $weekOfYear ISO-8601 week number of year, weeks starting on Monday
* @property-read integer $daysInMonth number of days in the given month
* @property-read integer $age does a diffInYears() with default parameters
* @property-read integer $quarter the quarter of this instance, 1 - 4
* @property-read integer $offset the timezone offset in seconds from UTC
* @property-read integer $offsetHours the timezone offset in hours from UTC
* @property-read boolean $dst daylight savings time indicator, true if DST, false otherwise
* @property-read boolean $local checks if the timezone is local, true if local, false otherwise
* @property-read boolean $utc checks if the timezone is UTC, true if UTC, false otherwise
* @property-read string $timezoneName
* @property-read string $tzName
*/
class Carbon extends DateTime
{
/**
* The day constants
*/
const SUNDAY = 0;
const MONDAY = 1;
const TUESDAY = 2;
const WEDNESDAY = 3;
const THURSDAY = 4;
const FRIDAY = 5;
const SATURDAY = 6;
/**
* Names of days of the week.
*
* @var array
*/
protected static $days = array(
self::SUNDAY => 'Sunday',
self::MONDAY => 'Monday',
self::TUESDAY => 'Tuesday',
self::WEDNESDAY => 'Wednesday',
self::THURSDAY => 'Thursday',
self::FRIDAY => 'Friday',
self::SATURDAY => 'Saturday',
);
/**
* Terms used to detect if a time passed is a relative date for testing purposes
*
* @var array
*/
protected static $relativeKeywords = array(
'this',
'next',
'last',
'tomorrow',
'yesterday',
'+',
'-',
'first',
'last',
'ago',
);
/**
* Number of X in Y
*/
const YEARS_PER_CENTURY = 100;
const YEARS_PER_DECADE = 10;
const MONTHS_PER_YEAR = 12;
const WEEKS_PER_YEAR = 52;
const DAYS_PER_WEEK = 7;
const HOURS_PER_DAY = 24;
const MINUTES_PER_HOUR = 60;
const SECONDS_PER_MINUTE = 60;
/**
* Default format to use for __toString method when type juggling occurs.
*
* @var string
*/
const DEFAULT_TO_STRING_FORMAT = 'Y-m-d H:i:s';
/**
* Format to use for __toString method when type juggling occurs.
*
* @var string
*/
protected static $toStringFormat = self::DEFAULT_TO_STRING_FORMAT;
/**
* First day of week
*
* @var int
*/
protected static $weekStartsAt = self::MONDAY;
/**
* Last day of week
*
* @var int
*/
protected static $weekEndsAt = self::SUNDAY;
/**
* Days of weekend
*
* @var array
*/
protected static $weekendDays = array(self::SATURDAY, self::SUNDAY);
/**
* A test Carbon instance to be returned when now instances are created
*
* @var Carbon
*/
protected static $testNow;
/**
* A translator to ... er ... translate stuff
*
* @var TranslatorInterface
*/
protected static $translator;
/**
* Creates a DateTimeZone from a string or a DateTimeZone
*
* @param DateTimeZone|string|null $object
*
* @return DateTimeZone
*
* @throws InvalidArgumentException
*/
protected static function safeCreateDateTimeZone($object)
{
if ($object === null) {
// Don't return null... avoid Bug #52063 in PHP <5.3.6
return new DateTimeZone(date_default_timezone_get());
}
if ($object instanceof DateTimeZone) {
return $object;
}
$tz = @timezone_open((string) $object);
if ($tz === false) {
throw new InvalidArgumentException('Unknown or bad timezone ('.$object.')');
}
return $tz;
}
///////////////////////////////////////////////////////////////////
//////////////////////////// CONSTRUCTORS /////////////////////////
///////////////////////////////////////////////////////////////////
/**
* Create a new Carbon instance.
*
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* @param string $time
* @param DateTimeZone|string $tz
*/
public function __construct($time = null, $tz = null)
{
// If the class has a test now set and we are trying to create a now()
// instance then override as required
if (static::hasTestNow() && (empty($time) || $time === 'now' || static::hasRelativeKeywords($time))) {
$testInstance = clone static::getTestNow();
if (static::hasRelativeKeywords($time)) {
$testInstance->modify($time);
}
//shift the time according to the given time zone
if ($tz !== NULL && $tz != static::getTestNow()->tz) {
$testInstance->setTimezone($tz);
} else {
$tz = $testInstance->tz;
}
$time = $testInstance->toDateTimeString();
}
parent::__construct($time, static::safeCreateDateTimeZone($tz));
}
/**
* Create a Carbon instance from a DateTime one
*
* @param DateTime $dt
*
* @return static
*/
public static function instance(DateTime $dt)
{
return new static($dt->format('Y-m-d H:i:s.u'), $dt->getTimeZone());
}
/**
* Create a carbon instance from a string. This is an alias for the
* constructor that allows better fluent syntax as it allows you to do
* Carbon::parse('Monday next week')->fn() rather than
* (new Carbon('Monday next week'))->fn()
*
* @param string $time
* @param DateTimeZone|string $tz
*
* @return static
*/
public static function parse($time = null, $tz = null)
{
return new static($time, $tz);
}
/**
* Get a Carbon instance for the current date and time
*
* @param DateTimeZone|string $tz
*
* @return static
*/
public static function now($tz = null)
{
return new static(null, $tz);
}
/**
* Create a Carbon instance for today
*
* @param DateTimeZone|string $tz
*
* @return static
*/
public static function today($tz = null)
{
return static::now($tz)->startOfDay();
}
/**
* Create a Carbon instance for tomorrow
*
* @param DateTimeZone|string $tz
*
* @return static
*/
public static function tomorrow($tz = null)
{
return static::today($tz)->addDay();
}
/**
* Create a Carbon instance for yesterday
*
* @param DateTimeZone|string $tz
*
* @return static
*/
public static function yesterday($tz = null)
{
return static::today($tz)->subDay();
}
/**
* Create a Carbon instance for the greatest supported date.
*
* @return Carbon
*/
public static function maxValue()
{
return static::createFromTimestamp(PHP_INT_MAX);
}
/**
* Create a Carbon instance for the lowest supported date.
*
* @return Carbon
*/
public static function minValue()
{
return static::createFromTimestamp(~PHP_INT_MAX);
}
/**
* Create a new Carbon instance from a specific date and time.
*
* If any of $year, $month or $day are set to null their now() values
* will be used.
*
* If $hour is null it will be set to its now() value and the default values
* for $minute and $second will be their now() values.
* If $hour is not null then the default values for $minute and $second
* will be 0.
*
* @param integer $year
* @param integer $month
* @param integer $day
* @param integer $hour
* @param integer $minute
* @param integer $second
* @param DateTimeZone|string $tz
*
* @return static
*/
public static function create($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null)
{
$year = ($year === null) ? date('Y') : $year;
$month = ($month === null) ? date('n') : $month;
$day = ($day === null) ? date('j') : $day;
if ($hour === null) {
$hour = date('G');
$minute = ($minute === null) ? date('i') : $minute;
$second = ($second === null) ? date('s') : $second;
} else {
$minute = ($minute === null) ? 0 : $minute;
$second = ($second === null) ? 0 : $second;
}
return static::createFromFormat('Y-n-j G:i:s', sprintf('%s-%s-%s %s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $tz);
}
/**
* Create a Carbon instance from just a date. The time portion is set to now.
*
* @param integer $year
* @param integer $month
* @param integer $day
* @param DateTimeZone|string $tz
*
* @return static
*/
public static function createFromDate($year = null, $month = null, $day = null, $tz = null)
{
return static::create($year, $month, $day, null, null, null, $tz);
}
/**
* Create a Carbon instance from just a time. The date portion is set to today.
*
* @param integer $hour
* @param integer $minute
* @param integer $second
* @param DateTimeZone|string $tz
*
* @return static
*/
public static function createFromTime($hour = null, $minute = null, $second = null, $tz = null)
{
return static::create(null, null, null, $hour, $minute, $second, $tz);
}
/**
* Create a Carbon instance from a specific format
*
* @param string $format
* @param string $time
* @param DateTimeZone|string $tz
*
* @return static
*
* @throws InvalidArgumentException
*/
public static function createFromFormat($format, $time, $tz = null)
{
if ($tz !== null) {
$dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
} else {
$dt = parent::createFromFormat($format, $time);
}
if ($dt instanceof DateTime) {
return static::instance($dt);
}
$errors = static::getLastErrors();
throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors']));
}
/**
* Create a Carbon instance from a timestamp
*
* @param integer $timestamp
* @param DateTimeZone|string $tz
*
* @return static
*/
public static function createFromTimestamp($timestamp, $tz = null)
{
return static::now($tz)->setTimestamp($timestamp);
}
/**
* Create a Carbon instance from an UTC timestamp
*
* @param integer $timestamp
*
* @return static
*/
public static function createFromTimestampUTC($timestamp)
{
return new static('@'.$timestamp);
}
/**
* Get a copy of the instance
*
* @return static
*/
public function copy()
{
return static::instance($this);
}
///////////////////////////////////////////////////////////////////
///////////////////////// GETTERS AND SETTERS /////////////////////
///////////////////////////////////////////////////////////////////
/**
* Get a part of the Carbon object
*
* @param string $name
*
* @throws InvalidArgumentException
*
* @return string|integer|DateTimeZone
*/
public function __get($name)
{
switch (true) {
case array_key_exists($name, $formats = array(
'year' => 'Y',
'yearIso' => 'o',
'month' => 'n',
'day' => 'j',
'hour' => 'G',
'minute' => 'i',
'second' => 's',
'micro' => 'u',
'dayOfWeek' => 'w',
'dayOfYear' => 'z',
'weekOfYear' => 'W',
'daysInMonth' => 't',
'timestamp' => 'U',
)):
return (int) $this->format($formats[$name]);
case $name === 'weekOfMonth':
return (int) ceil($this->day / static::DAYS_PER_WEEK);
case $name === 'age':
return (int) $this->diffInYears();
case $name === 'quarter':
return (int) ceil($this->month / 3);
case $name === 'offset':
return $this->getOffset();
case $name === 'offsetHours':
return $this->getOffset() / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR;
case $name === 'dst':
return $this->format('I') == '1';
case $name === 'local':
return $this->offset == $this->copy()->setTimezone(date_default_timezone_get())->offset;
case $name === 'utc':
return $this->offset == 0;
case $name === 'timezone' || $name === 'tz':
return $this->getTimezone();
case $name === 'timezoneName' || $name === 'tzName':
return $this->getTimezone()->getName();
default:
throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name));
}
}
/**
* Check if an attribute exists on the object
*
* @param string $name
*
* @return boolean
*/
public function __isset($name)
{
try {
$this->__get($name);
} catch (InvalidArgumentException $e) {
return false;
}
return true;
}
/**
* Set a part of the Carbon object
*
* @param string $name
* @param string|integer|DateTimeZone $value
*
* @throws InvalidArgumentException
*/
public function __set($name, $value)
{
switch ($name) {
case 'year':
$this->setDate($value, $this->month, $this->day);
break;
case 'month':
$this->setDate($this->year, $value, $this->day);
break;
case 'day':
$this->setDate($this->year, $this->month, $value);
break;
case 'hour':
$this->setTime($value, $this->minute, $this->second);
break;
case 'minute':
$this->setTime($this->hour, $value, $this->second);
break;
case 'second':
$this->setTime($this->hour, $this->minute, $value);
break;
case 'timestamp':
parent::setTimestamp($value);
break;
case 'timezone':
case 'tz':
$this->setTimezone($value);
break;
default:
throw new InvalidArgumentException(sprintf("Unknown setter '%s'", $name));
}
}
/**
* Set the instance's year
*
* @param integer $value
*
* @return static
*/
public function year($value)
{
$this->year = $value;
return $this;
}
/**
* Set the instance's month
*
* @param integer $value
*
* @return static
*/
public function month($value)
{
$this->month = $value;
return $this;
}
/**
* Set the instance's day
*
* @param integer $value
*
* @return static
*/
public function day($value)
{
$this->day = $value;
return $this;
}
/**
* Set the instance's hour
*
* @param integer $value
*
* @return static
*/
public function hour($value)
{
$this->hour = $value;
return $this;
}
/**
* Set the instance's minute
*
* @param integer $value
*
* @return static
*/
public function minute($value)
{
$this->minute = $value;
return $this;
}
/**
* Set the instance's second
*
* @param integer $value
*
* @return static
*/
public function second($value)
{
$this->second = $value;
return $this;
}
/**
* Set the date and time all together
*
* @param integer $year
* @param integer $month
* @param integer $day
* @param integer $hour
* @param integer $minute
* @param integer $second
*
* @return static
*/
public function setDateTime($year, $month, $day, $hour, $minute, $second = 0)
{
return $this->setDate($year, $month, $day)->setTime($hour, $minute, $second);
}
/**
* Set the instance's timestamp
*
* @param integer $value
*
* @return static
*/
public function timestamp($value)
{
$this->timestamp = $value;
return $this;
}
/**
* Alias for setTimezone()
*
* @param DateTimeZone|string $value
*
* @return static
*/
public function timezone($value)
{
return $this->setTimezone($value);
}
/**
* Alias for setTimezone()
*
* @param DateTimeZone|string $value
*
* @return static
*/
public function tz($value)
{
return $this->setTimezone($value);
}
/**
* Set the instance's timezone from a string or object
*
* @param DateTimeZone|string $value
*
* @return static
*/
public function setTimezone($value)
{
parent::setTimezone(static::safeCreateDateTimeZone($value));
return $this;
}
///////////////////////////////////////////////////////////////////
/////////////////////// WEEK SPECIAL DAYS /////////////////////////
///////////////////////////////////////////////////////////////////
/**
* Get the first day of week
*
* @return int
*/
public static function getWeekStartsAt()
{
return static::$weekStartsAt;
}
/**
* Set the first day of week
*
* @param int
*/
public static function setWeekStartsAt($day)
{
static::$weekStartsAt = $day;
}
/**
* Get the last day of week
*
* @return int
*/
public static function getWeekEndsAt()
{
return static::$weekEndsAt;
}
/**
* Set the first day of week
*
* @param int
*/
public static function setWeekEndsAt($day)
{
static::$weekEndsAt = $day;
}
/**
* Get weekend days
*
* @return array
*/
public static function getWeekendDays()
{
return static::$weekendDays;
}
/**
* Set weekend days
*
* @param array
*/
public static function setWeekendDays($days)
{
static::$weekendDays = $days;
}
///////////////////////////////////////////////////////////////////
///////////////////////// TESTING AIDS ////////////////////////////
///////////////////////////////////////////////////////////////////
/**
* Set a Carbon instance (real or mock) to be returned when a "now"
* instance is created. The provided instance will be returned
* specifically under the following conditions:
* - A call to the static now() method, ex. Carbon::now()
* - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null)
* - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now')
*
* Note the timezone parameter was left out of the examples above and
* has no affect as the mock value will be returned regardless of its value.
*
* To clear the test instance call this method using the default
* parameter of null.
*
* @param Carbon $testNow
*/
public static function setTestNow(Carbon $testNow = null)
{
static::$testNow = $testNow;
}
/**
* Get the Carbon instance (real or mock) to be returned when a "now"
* instance is created.
*
* @return static the current instance used for testing
*/
public static function getTestNow()
{
return static::$testNow;
}
/**
* Determine if there is a valid test instance set. A valid test instance
* is anything that is not null.
*
* @return boolean true if there is a test instance, otherwise false
*/
public static function hasTestNow()
{
return static::getTestNow() !== null;
}
/**
* Determine if there is a relative keyword in the time string, this is to
* create dates relative to now for test instances. e.g.: next tuesday
*
* @param string $time
*
* @return boolean true if there is a keyword, otherwise false
*/
public static function hasRelativeKeywords($time)
{
// skip common format with a '-' in it
if (preg_match('/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/', $time) !== 1) {
foreach (static::$relativeKeywords as $keyword) {
if (stripos($time, $keyword) !== false) {
return true;
}
}
}
return false;
}
///////////////////////////////////////////////////////////////////
/////////////////////// LOCALIZATION //////////////////////////////
///////////////////////////////////////////////////////////////////
/**
* Intialize the translator instance if necessary.
*
* @return TranslatorInterface
*/
protected static function translator()
{
if (static::$translator == null) {
static::$translator = new Translator('en');
static::$translator->addLoader('array', new ArrayLoader());
static::setLocale('en');
}
return static::$translator;
}
/**
* Get the translator instance in use
*
* @return TranslatorInterface
*/
public static function getTranslator()
{
return static::translator();
}
/**
* Set the translator instance to use
*
* @param TranslatorInterface $translator
*/
public static function setTranslator(TranslatorInterface $translator)
{
static::$translator = $translator;
}
/**
* Get the current translator locale
*
* @return string
*/
public static function getLocale()
{
return static::translator()->getLocale();
}
/**
* Set the current translator locale
*
* @param string $locale
*/
public static function setLocale($locale)
{
static::translator()->setLocale($locale);
// Ensure the locale has been loaded.
static::translator()->addResource('array', require __DIR__.'/Lang/'.$locale.'.php', $locale);
}
///////////////////////////////////////////////////////////////////
/////////////////////// STRING FORMATTING /////////////////////////
///////////////////////////////////////////////////////////////////
/**
* Format the instance with the current locale. You can set the current
* locale using setlocale() http://php.net/setlocale.
*
* @param string $format
*
* @return string
*/
public function formatLocalized($format)
{
// Check for Windows to find and replace the %e
// modifier correctly
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
$format = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format);
}
return strftime($format, strtotime($this));
}
/**
* Reset the format used to the default when type juggling a Carbon instance to a string
*
*/
public static function resetToStringFormat()
{
static::setToStringFormat(static::DEFAULT_TO_STRING_FORMAT);
}
/**
* Set the default format used when type juggling a Carbon instance to a string
*
* @param string $format
*/
public static function setToStringFormat($format)
{
static::$toStringFormat = $format;
}
/**
* Format the instance as a string using the set format
*
* @return string
*/
public function __toString()
{
return $this->format(static::$toStringFormat);
}
/**
* Format the instance as date
*
* @return string
*/
public function toDateString()
{
return $this->format('Y-m-d');
}
/**
* Format the instance as a readable date
*
* @return string
*/
public function toFormattedDateString()
{
return $this->format('M j, Y');
}
/**
* Format the instance as time
*
* @return string
*/
public function toTimeString()
{
return $this->format('H:i:s');
}
/**
* Format the instance as date and time
*
* @return string
*/
public function toDateTimeString()
{
return $this->format('Y-m-d H:i:s');
}
/**
* Format the instance with day, date and time
*
* @return string
*/
public function toDayDateTimeString()
{
return $this->format('D, M j, Y g:i A');
}
/**
* Format the instance as ATOM
*
* @return string
*/
public function toAtomString()
{
return $this->format(static::ATOM);
}
/**
* Format the instance as COOKIE
*
* @return string
*/
public function toCookieString()
{
return $this->format(static::COOKIE);
}
/**
* Format the instance as ISO8601
*
* @return string
*/
public function toIso8601String()
{
return $this->format(static::ISO8601);
}
/**
* Format the instance as RFC822
*
* @return string
*/
public function toRfc822String()
{
return $this->format(static::RFC822);
}
/**
* Format the instance as RFC850
*
* @return string
*/
public function toRfc850String()
{
return $this->format(static::RFC850);
}
/**
* Format the instance as RFC1036
*
* @return string
*/
public function toRfc1036String()
{
return $this->format(static::RFC1036);
}
/**
* Format the instance as RFC1123
*
* @return string
*/
public function toRfc1123String()
{
return $this->format(static::RFC1123);
}
/**
* Format the instance as RFC2822
*
* @return string
*/
public function toRfc2822String()
{
return $this->format(static::RFC2822);
}
/**
* Format the instance as RFC3339
*
* @return string
*/
public function toRfc3339String()
{
return $this->format(static::RFC3339);
}
/**
* Format the instance as RSS