Comparison.php
34.6 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
<?php
/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Carbon\Traits;
use BadMethodCallException;
use Carbon\CarbonInterface;
use Carbon\Exceptions\BadComparisonUnitException;
use InvalidArgumentException;
/**
* Trait Comparison.
*
* Comparison utils and testers. All the following methods return booleans.
* nowWithSameTz
*
* Depends on the following methods:
*
* @method static resolveCarbon($date)
* @method static copy()
* @method static nowWithSameTz()
* @method static static yesterday($timezone = null)
* @method static static tomorrow($timezone = null)
*/
trait Comparison
{
/** @var bool */
protected $endOfTime = false;
/** @var bool */
protected $startOfTime = false;
/**
* Determines if the instance is equal to another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->eq('2018-07-25 12:45:16'); // true
* Carbon::parse('2018-07-25 12:45:16')->eq(Carbon::parse('2018-07-25 12:45:16')); // true
* Carbon::parse('2018-07-25 12:45:16')->eq('2018-07-25 12:45:17'); // false
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @see equalTo()
*
* @return bool
*/
public function eq($date): bool
{
return $this->equalTo($date);
}
/**
* Determines if the instance is equal to another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->equalTo('2018-07-25 12:45:16'); // true
* Carbon::parse('2018-07-25 12:45:16')->equalTo(Carbon::parse('2018-07-25 12:45:16')); // true
* Carbon::parse('2018-07-25 12:45:16')->equalTo('2018-07-25 12:45:17'); // false
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @return bool
*/
public function equalTo($date): bool
{
$this->discourageNull($date);
$this->discourageBoolean($date);
return $this == $this->resolveCarbon($date);
}
/**
* Determines if the instance is not equal to another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->ne('2018-07-25 12:45:16'); // false
* Carbon::parse('2018-07-25 12:45:16')->ne(Carbon::parse('2018-07-25 12:45:16')); // false
* Carbon::parse('2018-07-25 12:45:16')->ne('2018-07-25 12:45:17'); // true
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @see notEqualTo()
*
* @return bool
*/
public function ne($date): bool
{
return $this->notEqualTo($date);
}
/**
* Determines if the instance is not equal to another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->notEqualTo('2018-07-25 12:45:16'); // false
* Carbon::parse('2018-07-25 12:45:16')->notEqualTo(Carbon::parse('2018-07-25 12:45:16')); // false
* Carbon::parse('2018-07-25 12:45:16')->notEqualTo('2018-07-25 12:45:17'); // true
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @return bool
*/
public function notEqualTo($date): bool
{
return !$this->equalTo($date);
}
/**
* Determines if the instance is greater (after) than another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->gt('2018-07-25 12:45:15'); // true
* Carbon::parse('2018-07-25 12:45:16')->gt('2018-07-25 12:45:16'); // false
* Carbon::parse('2018-07-25 12:45:16')->gt('2018-07-25 12:45:17'); // false
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @see greaterThan()
*
* @return bool
*/
public function gt($date): bool
{
return $this->greaterThan($date);
}
/**
* Determines if the instance is greater (after) than another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->greaterThan('2018-07-25 12:45:15'); // true
* Carbon::parse('2018-07-25 12:45:16')->greaterThan('2018-07-25 12:45:16'); // false
* Carbon::parse('2018-07-25 12:45:16')->greaterThan('2018-07-25 12:45:17'); // false
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @return bool
*/
public function greaterThan($date): bool
{
$this->discourageNull($date);
$this->discourageBoolean($date);
return $this > $this->resolveCarbon($date);
}
/**
* Determines if the instance is greater (after) than another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->isAfter('2018-07-25 12:45:15'); // true
* Carbon::parse('2018-07-25 12:45:16')->isAfter('2018-07-25 12:45:16'); // false
* Carbon::parse('2018-07-25 12:45:16')->isAfter('2018-07-25 12:45:17'); // false
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @see greaterThan()
*
* @return bool
*/
public function isAfter($date): bool
{
return $this->greaterThan($date);
}
/**
* Determines if the instance is greater (after) than or equal to another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->gte('2018-07-25 12:45:15'); // true
* Carbon::parse('2018-07-25 12:45:16')->gte('2018-07-25 12:45:16'); // true
* Carbon::parse('2018-07-25 12:45:16')->gte('2018-07-25 12:45:17'); // false
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @see greaterThanOrEqualTo()
*
* @return bool
*/
public function gte($date): bool
{
return $this->greaterThanOrEqualTo($date);
}
/**
* Determines if the instance is greater (after) than or equal to another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->greaterThanOrEqualTo('2018-07-25 12:45:15'); // true
* Carbon::parse('2018-07-25 12:45:16')->greaterThanOrEqualTo('2018-07-25 12:45:16'); // true
* Carbon::parse('2018-07-25 12:45:16')->greaterThanOrEqualTo('2018-07-25 12:45:17'); // false
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @return bool
*/
public function greaterThanOrEqualTo($date): bool
{
$this->discourageNull($date);
$this->discourageBoolean($date);
return $this >= $this->resolveCarbon($date);
}
/**
* Determines if the instance is less (before) than another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->lt('2018-07-25 12:45:15'); // false
* Carbon::parse('2018-07-25 12:45:16')->lt('2018-07-25 12:45:16'); // false
* Carbon::parse('2018-07-25 12:45:16')->lt('2018-07-25 12:45:17'); // true
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @see lessThan()
*
* @return bool
*/
public function lt($date): bool
{
return $this->lessThan($date);
}
/**
* Determines if the instance is less (before) than another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->lessThan('2018-07-25 12:45:15'); // false
* Carbon::parse('2018-07-25 12:45:16')->lessThan('2018-07-25 12:45:16'); // false
* Carbon::parse('2018-07-25 12:45:16')->lessThan('2018-07-25 12:45:17'); // true
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @return bool
*/
public function lessThan($date): bool
{
$this->discourageNull($date);
$this->discourageBoolean($date);
return $this < $this->resolveCarbon($date);
}
/**
* Determines if the instance is less (before) than another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->isBefore('2018-07-25 12:45:15'); // false
* Carbon::parse('2018-07-25 12:45:16')->isBefore('2018-07-25 12:45:16'); // false
* Carbon::parse('2018-07-25 12:45:16')->isBefore('2018-07-25 12:45:17'); // true
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @see lessThan()
*
* @return bool
*/
public function isBefore($date): bool
{
return $this->lessThan($date);
}
/**
* Determines if the instance is less (before) or equal to another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->lte('2018-07-25 12:45:15'); // false
* Carbon::parse('2018-07-25 12:45:16')->lte('2018-07-25 12:45:16'); // true
* Carbon::parse('2018-07-25 12:45:16')->lte('2018-07-25 12:45:17'); // true
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @see lessThanOrEqualTo()
*
* @return bool
*/
public function lte($date): bool
{
return $this->lessThanOrEqualTo($date);
}
/**
* Determines if the instance is less (before) or equal to another
*
* @example
* ```
* Carbon::parse('2018-07-25 12:45:16')->lessThanOrEqualTo('2018-07-25 12:45:15'); // false
* Carbon::parse('2018-07-25 12:45:16')->lessThanOrEqualTo('2018-07-25 12:45:16'); // true
* Carbon::parse('2018-07-25 12:45:16')->lessThanOrEqualTo('2018-07-25 12:45:17'); // true
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date
*
* @return bool
*/
public function lessThanOrEqualTo($date): bool
{
$this->discourageNull($date);
$this->discourageBoolean($date);
return $this <= $this->resolveCarbon($date);
}
/**
* Determines if the instance is between two others.
*
* The third argument allow you to specify if bounds are included or not (true by default)
* but for when you including/excluding bounds may produce different results in your application,
* we recommend to use the explicit methods ->betweenIncluded() or ->betweenExcluded() instead.
*
* @example
* ```
* Carbon::parse('2018-07-25')->between('2018-07-14', '2018-08-01'); // true
* Carbon::parse('2018-07-25')->between('2018-08-01', '2018-08-20'); // false
* Carbon::parse('2018-07-25')->between('2018-07-25', '2018-08-01'); // true
* Carbon::parse('2018-07-25')->between('2018-07-25', '2018-08-01', false); // false
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date1
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date2
* @param bool $equal Indicates if an equal to comparison should be done
*
* @return bool
*/
public function between($date1, $date2, $equal = true): bool
{
$date1 = $this->resolveCarbon($date1);
$date2 = $this->resolveCarbon($date2);
if ($date1->greaterThan($date2)) {
[$date1, $date2] = [$date2, $date1];
}
if ($equal) {
return $this >= $date1 && $this <= $date2;
}
return $this > $date1 && $this < $date2;
}
/**
* Determines if the instance is between two others, bounds included.
*
* @example
* ```
* Carbon::parse('2018-07-25')->betweenIncluded('2018-07-14', '2018-08-01'); // true
* Carbon::parse('2018-07-25')->betweenIncluded('2018-08-01', '2018-08-20'); // false
* Carbon::parse('2018-07-25')->betweenIncluded('2018-07-25', '2018-08-01'); // true
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date1
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date2
*
* @return bool
*/
public function betweenIncluded($date1, $date2): bool
{
return $this->between($date1, $date2, true);
}
/**
* Determines if the instance is between two others, bounds excluded.
*
* @example
* ```
* Carbon::parse('2018-07-25')->betweenExcluded('2018-07-14', '2018-08-01'); // true
* Carbon::parse('2018-07-25')->betweenExcluded('2018-08-01', '2018-08-20'); // false
* Carbon::parse('2018-07-25')->betweenExcluded('2018-07-25', '2018-08-01'); // false
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date1
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date2
*
* @return bool
*/
public function betweenExcluded($date1, $date2): bool
{
return $this->between($date1, $date2, false);
}
/**
* Determines if the instance is between two others
*
* @example
* ```
* Carbon::parse('2018-07-25')->isBetween('2018-07-14', '2018-08-01'); // true
* Carbon::parse('2018-07-25')->isBetween('2018-08-01', '2018-08-20'); // false
* Carbon::parse('2018-07-25')->isBetween('2018-07-25', '2018-08-01'); // true
* Carbon::parse('2018-07-25')->isBetween('2018-07-25', '2018-08-01', false); // false
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date1
* @param \Carbon\Carbon|\DateTimeInterface|mixed $date2
* @param bool $equal Indicates if an equal to comparison should be done
*
* @return bool
*/
public function isBetween($date1, $date2, $equal = true): bool
{
return $this->between($date1, $date2, $equal);
}
/**
* Determines if the instance is a weekday.
*
* @example
* ```
* Carbon::parse('2019-07-14')->isWeekday(); // false
* Carbon::parse('2019-07-15')->isWeekday(); // true
* ```
*
* @return bool
*/
public function isWeekday()
{
return !$this->isWeekend();
}
/**
* Determines if the instance is a weekend day.
*
* @example
* ```
* Carbon::parse('2019-07-14')->isWeekend(); // true
* Carbon::parse('2019-07-15')->isWeekend(); // false
* ```
*
* @return bool
*/
public function isWeekend()
{
return \in_array($this->dayOfWeek, static::$weekendDays, true);
}
/**
* Determines if the instance is yesterday.
*
* @example
* ```
* Carbon::yesterday()->isYesterday(); // true
* Carbon::tomorrow()->isYesterday(); // false
* ```
*
* @return bool
*/
public function isYesterday()
{
return $this->toDateString() === static::yesterday($this->getTimezone())->toDateString();
}
/**
* Determines if the instance is today.
*
* @example
* ```
* Carbon::today()->isToday(); // true
* Carbon::tomorrow()->isToday(); // false
* ```
*
* @return bool
*/
public function isToday()
{
return $this->toDateString() === $this->nowWithSameTz()->toDateString();
}
/**
* Determines if the instance is tomorrow.
*
* @example
* ```
* Carbon::tomorrow()->isTomorrow(); // true
* Carbon::yesterday()->isTomorrow(); // false
* ```
*
* @return bool
*/
public function isTomorrow()
{
return $this->toDateString() === static::tomorrow($this->getTimezone())->toDateString();
}
/**
* Determines if the instance is in the future, ie. greater (after) than now.
*
* @example
* ```
* Carbon::now()->addHours(5)->isFuture(); // true
* Carbon::now()->subHours(5)->isFuture(); // false
* ```
*
* @return bool
*/
public function isFuture()
{
return $this->greaterThan($this->nowWithSameTz());
}
/**
* Determines if the instance is in the past, ie. less (before) than now.
*
* @example
* ```
* Carbon::now()->subHours(5)->isPast(); // true
* Carbon::now()->addHours(5)->isPast(); // false
* ```
*
* @return bool
*/
public function isPast()
{
return $this->lessThan($this->nowWithSameTz());
}
/**
* Determines if the instance is a leap year.
*
* @example
* ```
* Carbon::parse('2020-01-01')->isLeapYear(); // true
* Carbon::parse('2019-01-01')->isLeapYear(); // false
* ```
*
* @return bool
*/
public function isLeapYear()
{
return $this->rawFormat('L') === '1';
}
/**
* Determines if the instance is a long year (using calendar year).
*
* ⚠️ This method completely ignores month and day to use the numeric year number,
* it's not correct if the exact date matters. For instance as `2019-12-30` is already
* in the first week of the 2020 year, if you want to know from this date if ISO week
* year 2020 is a long year, use `isLongIsoYear` instead.
*
* @example
* ```
* Carbon::create(2015)->isLongYear(); // true
* Carbon::create(2016)->isLongYear(); // false
* ```
*
* @see https://en.wikipedia.org/wiki/ISO_8601#Week_dates
*
* @return bool
*/
public function isLongYear()
{
return static::create($this->year, 12, 28, 0, 0, 0, $this->tz)->weekOfYear === 53;
}
/**
* Determines if the instance is a long year (using ISO 8601 year).
*
* @example
* ```
* Carbon::parse('2015-01-01')->isLongIsoYear(); // true
* Carbon::parse('2016-01-01')->isLongIsoYear(); // true
* Carbon::parse('2016-01-03')->isLongIsoYear(); // false
* Carbon::parse('2019-12-29')->isLongIsoYear(); // false
* Carbon::parse('2019-12-30')->isLongIsoYear(); // true
* ```
*
* @see https://en.wikipedia.org/wiki/ISO_8601#Week_dates
*
* @return bool
*/
public function isLongIsoYear()
{
return static::create($this->isoWeekYear, 12, 28, 0, 0, 0, $this->tz)->weekOfYear === 53;
}
/**
* Compares the formatted values of the two dates.
*
* @example
* ```
* Carbon::parse('2019-06-13')->isSameAs('Y-d', Carbon::parse('2019-12-13')); // true
* Carbon::parse('2019-06-13')->isSameAs('Y-d', Carbon::parse('2019-06-14')); // false
* ```
*
* @param string $format date formats to compare.
* @param \Carbon\Carbon|\DateTimeInterface|string|null $date instance to compare with or null to use current day.
*
* @return bool
*/
public function isSameAs($format, $date = null)
{
return $this->rawFormat($format) === $this->resolveCarbon($date)->rawFormat($format);
}
/**
* Determines if the instance is in the current unit given.
*
* @example
* ```
* Carbon::parse('2019-01-13')->isSameUnit('year', Carbon::parse('2019-12-25')); // true
* Carbon::parse('2018-12-13')->isSameUnit('year', Carbon::parse('2019-12-25')); // false
* ```
*
* @param string $unit singular unit string
* @param \Carbon\Carbon|\DateTimeInterface|null $date instance to compare with or null to use current day.
*
* @throws BadComparisonUnitException
*
* @return bool
*/
public function isSameUnit($unit, $date = null)
{
$units = [
// @call isSameUnit
'year' => 'Y',
// @call isSameUnit
'week' => 'o-W',
// @call isSameUnit
'day' => 'Y-m-d',
// @call isSameUnit
'hour' => 'Y-m-d H',
// @call isSameUnit
'minute' => 'Y-m-d H:i',
// @call isSameUnit
'second' => 'Y-m-d H:i:s',
// @call isSameUnit
'micro' => 'Y-m-d H:i:s.u',
// @call isSameUnit
'microsecond' => 'Y-m-d H:i:s.u',
];
if (isset($units[$unit])) {
return $this->isSameAs($units[$unit], $date);
}
if (isset($this->$unit)) {
return $this->resolveCarbon($date)->$unit === $this->$unit;
}
if ($this->localStrictModeEnabled ?? static::isStrictModeEnabled()) {
throw new BadComparisonUnitException($unit);
}
return false;
}
/**
* Determines if the instance is in the current unit given.
*
* @example
* ```
* Carbon::now()->isCurrentUnit('hour'); // true
* Carbon::now()->subHours(2)->isCurrentUnit('hour'); // false
* ```
*
* @param string $unit The unit to test.
*
* @throws BadMethodCallException
*
* @return bool
*/
public function isCurrentUnit($unit)
{
return $this->{'isSame'.ucfirst($unit)}();
}
/**
* Checks if the passed in date is in the same quarter as the instance quarter (and year if needed).
*
* @example
* ```
* Carbon::parse('2019-01-12')->isSameQuarter(Carbon::parse('2019-03-01')); // true
* Carbon::parse('2019-01-12')->isSameQuarter(Carbon::parse('2019-04-01')); // false
* Carbon::parse('2019-01-12')->isSameQuarter(Carbon::parse('2018-03-01')); // false
* Carbon::parse('2019-01-12')->isSameQuarter(Carbon::parse('2018-03-01'), false); // true
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|string|null $date The instance to compare with or null to use current day.
* @param bool $ofSameYear Check if it is the same month in the same year.
*
* @return bool
*/
public function isSameQuarter($date = null, $ofSameYear = true)
{
$date = $this->resolveCarbon($date);
return $this->quarter === $date->quarter && (!$ofSameYear || $this->isSameYear($date));
}
/**
* Checks if the passed in date is in the same month as the instance´s month.
*
* @example
* ```
* Carbon::parse('2019-01-12')->isSameMonth(Carbon::parse('2019-01-01')); // true
* Carbon::parse('2019-01-12')->isSameMonth(Carbon::parse('2019-02-01')); // false
* Carbon::parse('2019-01-12')->isSameMonth(Carbon::parse('2018-01-01')); // false
* Carbon::parse('2019-01-12')->isSameMonth(Carbon::parse('2018-01-01'), false); // true
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|null $date The instance to compare with or null to use the current date.
* @param bool $ofSameYear Check if it is the same month in the same year.
*
* @return bool
*/
public function isSameMonth($date = null, $ofSameYear = true)
{
return $this->isSameAs($ofSameYear ? 'Y-m' : 'm', $date);
}
/**
* Checks if this day is a specific day of the week.
*
* @example
* ```
* Carbon::parse('2019-07-17')->isDayOfWeek(Carbon::WEDNESDAY); // true
* Carbon::parse('2019-07-17')->isDayOfWeek(Carbon::FRIDAY); // false
* Carbon::parse('2019-07-17')->isDayOfWeek('Wednesday'); // true
* Carbon::parse('2019-07-17')->isDayOfWeek('Friday'); // false
* ```
*
* @param int $dayOfWeek
*
* @return bool
*/
public function isDayOfWeek($dayOfWeek)
{
if (\is_string($dayOfWeek) && \defined($constant = static::class.'::'.strtoupper($dayOfWeek))) {
$dayOfWeek = \constant($constant);
}
return $this->dayOfWeek === $dayOfWeek;
}
/**
* Check if its the birthday. Compares the date/month values of the two dates.
*
* @example
* ```
* Carbon::now()->subYears(5)->isBirthday(); // true
* Carbon::now()->subYears(5)->subDay()->isBirthday(); // false
* Carbon::parse('2019-06-05')->isBirthday(Carbon::parse('2001-06-05')); // true
* Carbon::parse('2019-06-05')->isBirthday(Carbon::parse('2001-06-06')); // false
* ```
*
* @param \Carbon\Carbon|\DateTimeInterface|null $date The instance to compare with or null to use current day.
*
* @return bool
*/
public function isBirthday($date = null)
{
return $this->isSameAs('md', $date);
}
/**
* Check if today is the last day of the Month
*
* @example
* ```
* Carbon::parse('2019-02-28')->isLastOfMonth(); // true
* Carbon::parse('2019-03-28')->isLastOfMonth(); // false
* Carbon::parse('2019-03-30')->isLastOfMonth(); // false
* Carbon::parse('2019-03-31')->isLastOfMonth(); // true
* Carbon::parse('2019-04-30')->isLastOfMonth(); // true
* ```
*
* @return bool
*/
public function isLastOfMonth()
{
return $this->day === $this->daysInMonth;
}
/**
* Check if the instance is start of day / midnight.
*
* @example
* ```
* Carbon::parse('2019-02-28 00:00:00')->isStartOfDay(); // true
* Carbon::parse('2019-02-28 00:00:00.999999')->isStartOfDay(); // true
* Carbon::parse('2019-02-28 00:00:01')->isStartOfDay(); // false
* Carbon::parse('2019-02-28 00:00:00.000000')->isStartOfDay(true); // true
* Carbon::parse('2019-02-28 00:00:00.000012')->isStartOfDay(true); // false
* ```
*
* @param bool $checkMicroseconds check time at microseconds precision
*
* @return bool
*/
public function isStartOfDay($checkMicroseconds = false)
{
/* @var CarbonInterface $this */
return $checkMicroseconds
? $this->rawFormat('H:i:s.u') === '00:00:00.000000'
: $this->rawFormat('H:i:s') === '00:00:00';
}
/**
* Check if the instance is end of day.
*
* @example
* ```
* Carbon::parse('2019-02-28 23:59:59.999999')->isEndOfDay(); // true
* Carbon::parse('2019-02-28 23:59:59.123456')->isEndOfDay(); // true
* Carbon::parse('2019-02-28 23:59:59')->isEndOfDay(); // true
* Carbon::parse('2019-02-28 23:59:58.999999')->isEndOfDay(); // false
* Carbon::parse('2019-02-28 23:59:59.999999')->isEndOfDay(true); // true
* Carbon::parse('2019-02-28 23:59:59.123456')->isEndOfDay(true); // false
* Carbon::parse('2019-02-28 23:59:59')->isEndOfDay(true); // false
* ```
*
* @param bool $checkMicroseconds check time at microseconds precision
*
* @return bool
*/
public function isEndOfDay($checkMicroseconds = false)
{
/* @var CarbonInterface $this */
return $checkMicroseconds
? $this->rawFormat('H:i:s.u') === '23:59:59.999999'
: $this->rawFormat('H:i:s') === '23:59:59';
}
/**
* Check if the instance is start of day / midnight.
*
* @example
* ```
* Carbon::parse('2019-02-28 00:00:00')->isMidnight(); // true
* Carbon::parse('2019-02-28 00:00:00.999999')->isMidnight(); // true
* Carbon::parse('2019-02-28 00:00:01')->isMidnight(); // false
* ```
*
* @return bool
*/
public function isMidnight()
{
return $this->isStartOfDay();
}
/**
* Check if the instance is midday.
*
* @example
* ```
* Carbon::parse('2019-02-28 11:59:59.999999')->isMidday(); // false
* Carbon::parse('2019-02-28 12:00:00')->isMidday(); // true
* Carbon::parse('2019-02-28 12:00:00.999999')->isMidday(); // true
* Carbon::parse('2019-02-28 12:00:01')->isMidday(); // false
* ```
*
* @return bool
*/
public function isMidday()
{
/* @var CarbonInterface $this */
return $this->rawFormat('G:i:s') === static::$midDayAt.':00:00';
}
/**
* Checks if the (date)time string is in a given format.
*
* @example
* ```
* Carbon::hasFormat('11:12:45', 'h:i:s'); // true
* Carbon::hasFormat('13:12:45', 'h:i:s'); // false
* ```
*
* @param string $date
* @param string $format
*
* @return bool
*/
public static function hasFormat($date, $format)
{
// createFromFormat() is known to handle edge cases silently.
// E.g. "1975-5-1" (Y-n-j) will still be parsed correctly when "Y-m-d" is supplied as the format.
// To ensure we're really testing against our desired format, perform an additional regex validation.
return self::matchFormatPattern((string) $date, preg_quote((string) $format, '/'), static::$regexFormats);
}
/**
* Checks if the (date)time string is in a given format.
*
* @example
* ```
* Carbon::hasFormatWithModifiers('31/08/2015', 'd#m#Y'); // true
* Carbon::hasFormatWithModifiers('31/08/2015', 'm#d#Y'); // false
* ```
*
* @param string $date
* @param string $format
*
* @return bool
*/
public static function hasFormatWithModifiers($date, $format): bool
{
return self::matchFormatPattern((string) $date, (string) $format, array_merge(static::$regexFormats, static::$regexFormatModifiers));
}
/**
* Checks if the (date)time string is in a given format and valid to create a
* new instance.
*
* @example
* ```
* Carbon::canBeCreatedFromFormat('11:12:45', 'h:i:s'); // true
* Carbon::canBeCreatedFromFormat('13:12:45', 'h:i:s'); // false
* ```
*
* @param string $date
* @param string $format
*
* @return bool
*/
public static function canBeCreatedFromFormat($date, $format)
{
try {
// Try to create a DateTime object. Throws an InvalidArgumentException if the provided time string
// doesn't match the format in any way.
if (!static::rawCreateFromFormat($format, $date)) {
return false;
}
} catch (InvalidArgumentException $e) {
return false;
}
return static::hasFormatWithModifiers($date, $format);
}
/**
* Returns true if the current date matches the given string.
*
* @example
* ```
* var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2019')); // true
* var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2018')); // false
* var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2019-06')); // true
* var_dump(Carbon::parse('2019-06-02 12:23:45')->is('06-02')); // true
* var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-02')); // true
* var_dump(Carbon::parse('2019-06-02 12:23:45')->is('Sunday')); // true
* var_dump(Carbon::parse('2019-06-02 12:23:45')->is('June')); // true
* var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12:23')); // true
* var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12:23:45')); // true
* var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12:23:00')); // false
* var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12h')); // true
* var_dump(Carbon::parse('2019-06-02 15:23:45')->is('3pm')); // true
* var_dump(Carbon::parse('2019-06-02 15:23:45')->is('3am')); // false
* ```
*
* @param string $tester day name, month name, hour, date, etc. as string
*
* @return bool
*/
public function is(string $tester)
{
$tester = trim($tester);
if (preg_match('/^\d+$/', $tester)) {
return $this->year === (int) $tester;
}
if (preg_match('/^\d{3,}-\d{1,2}$/', $tester)) {
return $this->isSameMonth(static::parse($tester));
}
if (preg_match('/^\d{1,2}-\d{1,2}$/', $tester)) {
return $this->isSameDay(static::parse($this->year.'-'.$tester));
}
$modifier = preg_replace('/(\d)h$/i', '$1:00', $tester);
/* @var CarbonInterface $max */
$median = static::parse('5555-06-15 12:30:30.555555')->modify($modifier);
$current = $this->avoidMutation();
/* @var CarbonInterface $other */
$other = $this->avoidMutation()->modify($modifier);
if ($current->eq($other)) {
return true;
}
if (preg_match('/\d:\d{1,2}:\d{1,2}$/', $tester)) {
return $current->startOfSecond()->eq($other);
}
if (preg_match('/\d:\d{1,2}$/', $tester)) {
return $current->startOfMinute()->eq($other);
}
if (preg_match('/\d(?:h|am|pm)$/', $tester)) {
return $current->startOfHour()->eq($other);
}
if (preg_match(
'/^(?:january|february|march|april|may|june|july|august|september|october|november|december)(?:\s+\d+)?$/i',
$tester
)) {
return $current->startOfMonth()->eq($other->startOfMonth());
}
$units = [
'month' => [1, 'year'],
'day' => [1, 'month'],
'hour' => [0, 'day'],
'minute' => [0, 'hour'],
'second' => [0, 'minute'],
'microsecond' => [0, 'second'],
];
foreach ($units as $unit => [$minimum, $startUnit]) {
if ($minimum === $median->$unit) {
$current = $current->startOf($startUnit);
break;
}
}
return $current->eq($other);
}
/**
* Checks if the (date)time string is in a given format with
* given list of pattern replacements.
*
* @example
* ```
* Carbon::hasFormat('11:12:45', 'h:i:s'); // true
* Carbon::hasFormat('13:12:45', 'h:i:s'); // false
* ```
*
* @param string $date
* @param string $format
* @param array $replacements
*
* @return bool
*/
private static function matchFormatPattern(string $date, string $format, array $replacements): bool
{
// Preg quote, but remove escaped backslashes since we'll deal with escaped characters in the format string.
$regex = str_replace('\\\\', '\\', $format);
// Replace not-escaped letters
$regex = preg_replace_callback(
'/(?<!\\\\)((?:\\\\{2})*)(['.implode('', array_keys($replacements)).'])/',
function ($match) use ($replacements) {
return $match[1].strtr($match[2], $replacements);
},
$regex
);
// Replace escaped letters by the letter itself
$regex = preg_replace('/(?<!\\\\)((?:\\\\{2})*)\\\\(\w)/', '$1$2', $regex);
// Escape not escaped slashes
$regex = preg_replace('#(?<!\\\\)((?:\\\\{2})*)/#', '$1\\/', $regex);
return (bool) @preg_match('/^'.$regex.'$/', $date);
}
/**
* Returns true if the date was created using CarbonImmutable::startOfTime()
*
* @return bool
*/
public function isStartOfTime(): bool
{
return $this->startOfTime ?? false;
}
/**
* Returns true if the date was created using CarbonImmutable::endOfTime()
*
* @return bool
*/
public function isEndOfTime(): bool
{
return $this->endOfTime ?? false;
}
private function discourageNull($value): void
{
if ($value === null) {
@trigger_error("Since 2.61.0, it's deprecated to compare a date to null, meaning of such comparison is ambiguous and will no longer be possible in 3.0.0, you should explicitly pass 'now' or make an other check to eliminate null values.", \E_USER_DEPRECATED);
}
}
private function discourageBoolean($value): void
{
if (\is_bool($value)) {
@trigger_error("Since 2.61.0, it's deprecated to compare a date to true or false, meaning of such comparison is ambiguous and will no longer be possible in 3.0.0, you should explicitly pass 'now' or make an other check to eliminate boolean values.", \E_USER_DEPRECATED);
}
}
}