Debian Codegraph @

lib/3rdParty/composer/nesbot/carbon/src/Carbon/Traits/Comparison.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * This file is part of the Carbon package.
7 *
8 * (c) Brian Nesbitt <[email protected]>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14 namespace Carbon\Traits;
15
16 use BackedEnum;
17 use BadMethodCallException;
18 use Carbon\CarbonConverterInterface;
19 use Carbon\CarbonInterface;
20 use Carbon\Exceptions\BadComparisonUnitException;
21 use Carbon\FactoryImmutable;
22 use Carbon\Month;
23 use Carbon\Unit;
24 use Carbon\WeekDay;
25 use Closure;
26 use DateInterval;
27 use DateTimeInterface;
28 use InvalidArgumentException;
29
30 /**
31 * Trait Comparison.
32 *
33 * Comparison utils and testers. All the following methods return booleans.
34 * nowWithSameTz
35 *
36 * Depends on the following methods:
37 *
38 * @method static resolveCarbon($date)
39 * @method static copy()
40 * @method static nowWithSameTz()
41 * @method static static yesterday($timezone = null)
42 * @method static static tomorrow($timezone = null)
43 */
44 trait Comparison
45 {
46 protected bool $endOfTime = false;
47
48 protected bool $startOfTime = false;
49
50 /**
51 * Determines if the instance is equal to another
52 *
53 * @example
54 * ```
55 * Carbon::parse('2018-07-25 12:45:16')->eq('2018-07-25 12:45:16'); // true
56 * Carbon::parse('2018-07-25 12:45:16')->eq(Carbon::parse('2018-07-25 12:45:16')); // true
57 * Carbon::parse('2018-07-25 12:45:16')->eq('2018-07-25 12:45:17'); // false
58 * ```
59 *
60 * @see equalTo()
61 */
62 public function eq(DateTimeInterface|string $date): bool
63 {
64 return $this->equalTo($date);
65 }
66
67 /**
68 * Determines if the instance is equal to another
69 *
70 * @example
71 * ```
72 * Carbon::parse('2018-07-25 12:45:16')->equalTo('2018-07-25 12:45:16'); // true
73 * Carbon::parse('2018-07-25 12:45:16')->equalTo(Carbon::parse('2018-07-25 12:45:16')); // true
74 * Carbon::parse('2018-07-25 12:45:16')->equalTo('2018-07-25 12:45:17'); // false
75 * ```
76 */
77 public function equalTo(DateTimeInterface|string $date): bool
78 {
79 return $this == $this->resolveCarbon($date);
80 }
81
82 /**
83 * Determines if the instance is not equal to another
84 *
85 * @example
86 * ```
87 * Carbon::parse('2018-07-25 12:45:16')->ne('2018-07-25 12:45:16'); // false
88 * Carbon::parse('2018-07-25 12:45:16')->ne(Carbon::parse('2018-07-25 12:45:16')); // false
89 * Carbon::parse('2018-07-25 12:45:16')->ne('2018-07-25 12:45:17'); // true
90 * ```
91 *
92 * @see notEqualTo()
93 */
94 public function ne(DateTimeInterface|string $date): bool
95 {
96 return $this->notEqualTo($date);
97 }
98
99 /**
100 * Determines if the instance is not equal to another
101 *
102 * @example
103 * ```
104 * Carbon::parse('2018-07-25 12:45:16')->notEqualTo('2018-07-25 12:45:16'); // false
105 * Carbon::parse('2018-07-25 12:45:16')->notEqualTo(Carbon::parse('2018-07-25 12:45:16')); // false
106 * Carbon::parse('2018-07-25 12:45:16')->notEqualTo('2018-07-25 12:45:17'); // true
107 * ```
108 */
109 public function notEqualTo(DateTimeInterface|string $date): bool
110 {
111 return !$this->equalTo($date);
112 }
113
114 /**
115 * Determines if the instance is greater (after) than another
116 *
117 * @example
118 * ```
119 * Carbon::parse('2018-07-25 12:45:16')->gt('2018-07-25 12:45:15'); // true
120 * Carbon::parse('2018-07-25 12:45:16')->gt('2018-07-25 12:45:16'); // false
121 * Carbon::parse('2018-07-25 12:45:16')->gt('2018-07-25 12:45:17'); // false
122 * ```
123 *
124 * @see greaterThan()
125 */
126 public function gt(DateTimeInterface|string $date): bool
127 {
128 return $this->greaterThan($date);
129 }
130
131 /**
132 * Determines if the instance is greater (after) than another
133 *
134 * @example
135 * ```
136 * Carbon::parse('2018-07-25 12:45:16')->greaterThan('2018-07-25 12:45:15'); // true
137 * Carbon::parse('2018-07-25 12:45:16')->greaterThan('2018-07-25 12:45:16'); // false
138 * Carbon::parse('2018-07-25 12:45:16')->greaterThan('2018-07-25 12:45:17'); // false
139 * ```
140 */
141 public function greaterThan(DateTimeInterface|string $date): bool
142 {
143 return $this > $this->resolveCarbon($date);
144 }
145
146 /**
147 * Determines if the instance is greater (after) than another
148 *
149 * @example
150 * ```
151 * Carbon::parse('2018-07-25 12:45:16')->isAfter('2018-07-25 12:45:15'); // true
152 * Carbon::parse('2018-07-25 12:45:16')->isAfter('2018-07-25 12:45:16'); // false
153 * Carbon::parse('2018-07-25 12:45:16')->isAfter('2018-07-25 12:45:17'); // false
154 * ```
155 *
156 * @see greaterThan()
157 */
158 public function isAfter(DateTimeInterface|string $date): bool
159 {
160 return $this->greaterThan($date);
161 }
162
163 /**
164 * Determines if the instance is greater (after) than or equal to another
165 *
166 * @example
167 * ```
168 * Carbon::parse('2018-07-25 12:45:16')->gte('2018-07-25 12:45:15'); // true
169 * Carbon::parse('2018-07-25 12:45:16')->gte('2018-07-25 12:45:16'); // true
170 * Carbon::parse('2018-07-25 12:45:16')->gte('2018-07-25 12:45:17'); // false
171 * ```
172 *
173 * @see greaterThanOrEqualTo()
174 */
175 public function gte(DateTimeInterface|string $date): bool
176 {
177 return $this->greaterThanOrEqualTo($date);
178 }
179
180 /**
181 * Determines if the instance is greater (after) than or equal to another
182 *
183 * @example
184 * ```
185 * Carbon::parse('2018-07-25 12:45:16')->greaterThanOrEqualTo('2018-07-25 12:45:15'); // true
186 * Carbon::parse('2018-07-25 12:45:16')->greaterThanOrEqualTo('2018-07-25 12:45:16'); // true
187 * Carbon::parse('2018-07-25 12:45:16')->greaterThanOrEqualTo('2018-07-25 12:45:17'); // false
188 * ```
189 */
190 public function greaterThanOrEqualTo(DateTimeInterface|string $date): bool
191 {
192 return $this >= $this->resolveCarbon($date);
193 }
194
195 /**
196 * Determines if the instance is less (before) than another
197 *
198 * @example
199 * ```
200 * Carbon::parse('2018-07-25 12:45:16')->lt('2018-07-25 12:45:15'); // false
201 * Carbon::parse('2018-07-25 12:45:16')->lt('2018-07-25 12:45:16'); // false
202 * Carbon::parse('2018-07-25 12:45:16')->lt('2018-07-25 12:45:17'); // true
203 * ```
204 *
205 * @see lessThan()
206 */
207 public function lt(DateTimeInterface|string $date): bool
208 {
209 return $this->lessThan($date);
210 }
211
212 /**
213 * Determines if the instance is less (before) than another
214 *
215 * @example
216 * ```
217 * Carbon::parse('2018-07-25 12:45:16')->lessThan('2018-07-25 12:45:15'); // false
218 * Carbon::parse('2018-07-25 12:45:16')->lessThan('2018-07-25 12:45:16'); // false
219 * Carbon::parse('2018-07-25 12:45:16')->lessThan('2018-07-25 12:45:17'); // true
220 * ```
221 */
222 public function lessThan(DateTimeInterface|string $date): bool
223 {
224 return $this < $this->resolveCarbon($date);
225 }
226
227 /**
228 * Determines if the instance is less (before) than another
229 *
230 * @example
231 * ```
232 * Carbon::parse('2018-07-25 12:45:16')->isBefore('2018-07-25 12:45:15'); // false
233 * Carbon::parse('2018-07-25 12:45:16')->isBefore('2018-07-25 12:45:16'); // false
234 * Carbon::parse('2018-07-25 12:45:16')->isBefore('2018-07-25 12:45:17'); // true
235 * ```
236 *
237 * @see lessThan()
238 */
239 public function isBefore(DateTimeInterface|string $date): bool
240 {
241 return $this->lessThan($date);
242 }
243
244 /**
245 * Determines if the instance is less (before) or equal to another
246 *
247 * @example
248 * ```
249 * Carbon::parse('2018-07-25 12:45:16')->lte('2018-07-25 12:45:15'); // false
250 * Carbon::parse('2018-07-25 12:45:16')->lte('2018-07-25 12:45:16'); // true
251 * Carbon::parse('2018-07-25 12:45:16')->lte('2018-07-25 12:45:17'); // true
252 * ```
253 *
254 * @see lessThanOrEqualTo()
255 */
256 public function lte(DateTimeInterface|string $date): bool
257 {
258 return $this->lessThanOrEqualTo($date);
259 }
260
261 /**
262 * Determines if the instance is less (before) or equal to another
263 *
264 * @example
265 * ```
266 * Carbon::parse('2018-07-25 12:45:16')->lessThanOrEqualTo('2018-07-25 12:45:15'); // false
267 * Carbon::parse('2018-07-25 12:45:16')->lessThanOrEqualTo('2018-07-25 12:45:16'); // true
268 * Carbon::parse('2018-07-25 12:45:16')->lessThanOrEqualTo('2018-07-25 12:45:17'); // true
269 * ```
270 */
271 public function lessThanOrEqualTo(DateTimeInterface|string $date): bool
272 {
273 return $this <= $this->resolveCarbon($date);
274 }
275
276 /**
277 * Determines if the instance is between two others.
278 *
279 * The third argument allow you to specify if bounds are included or not (true by default)
280 * but for when you including/excluding bounds may produce different results in your application,
281 * we recommend to use the explicit methods ->betweenIncluded() or ->betweenExcluded() instead.
282 *
283 * @example
284 * ```
285 * Carbon::parse('2018-07-25')->between('2018-07-14', '2018-08-01'); // true
286 * Carbon::parse('2018-07-25')->between('2018-08-01', '2018-08-20'); // false
287 * Carbon::parse('2018-07-25')->between('2018-07-25', '2018-08-01'); // true
288 * Carbon::parse('2018-07-25')->between('2018-07-25', '2018-08-01', false); // false
289 * ```
290 *
291 * @param bool $equal Indicates if an equal to comparison should be done
292 */
293 public function between(DateTimeInterface|string $date1, DateTimeInterface|string $date2, bool $equal = true): bool
294 {
295 $date1 = $this->resolveCarbon($date1);
296 $date2 = $this->resolveCarbon($date2);
297
298 if ($date1->greaterThan($date2)) {
299 [$date1, $date2] = [$date2, $date1];
300 }
301
302 if ($equal) {
303 return $this >= $date1 && $this <= $date2;
304 }
305
306 return $this > $date1 && $this < $date2;
307 }
308
309 /**
310 * Determines if the instance is between two others, bounds included.
311 *
312 * @example
313 * ```
314 * Carbon::parse('2018-07-25')->betweenIncluded('2018-07-14', '2018-08-01'); // true
315 * Carbon::parse('2018-07-25')->betweenIncluded('2018-08-01', '2018-08-20'); // false
316 * Carbon::parse('2018-07-25')->betweenIncluded('2018-07-25', '2018-08-01'); // true
317 * ```
318 */
319 public function betweenIncluded(DateTimeInterface|string $date1, DateTimeInterface|string $date2): bool
320 {
321 return $this->between($date1, $date2, true);
322 }
323
324 /**
325 * Determines if the instance is between two others, bounds excluded.
326 *
327 * @example
328 * ```
329 * Carbon::parse('2018-07-25')->betweenExcluded('2018-07-14', '2018-08-01'); // true
330 * Carbon::parse('2018-07-25')->betweenExcluded('2018-08-01', '2018-08-20'); // false
331 * Carbon::parse('2018-07-25')->betweenExcluded('2018-07-25', '2018-08-01'); // false
332 * ```
333 */
334 public function betweenExcluded(DateTimeInterface|string $date1, DateTimeInterface|string $date2): bool
335 {
336 return $this->between($date1, $date2, false);
337 }
338
339 /**
340 * Determines if the instance is between two others
341 *
342 * @example
343 * ```
344 * Carbon::parse('2018-07-25')->isBetween('2018-07-14', '2018-08-01'); // true
345 * Carbon::parse('2018-07-25')->isBetween('2018-08-01', '2018-08-20'); // false
346 * Carbon::parse('2018-07-25')->isBetween('2018-07-25', '2018-08-01'); // true
347 * Carbon::parse('2018-07-25')->isBetween('2018-07-25', '2018-08-01', false); // false
348 * ```
349 *
350 * @param bool $equal Indicates if an equal to comparison should be done
351 */
352 public function isBetween(DateTimeInterface|string $date1, DateTimeInterface|string $date2, bool $equal = true): bool
353 {
354 return $this->between($date1, $date2, $equal);
355 }
356
357 /**
358 * Determines if the instance is a weekday.
359 *
360 * @example
361 * ```
362 * Carbon::parse('2019-07-14')->isWeekday(); // false
363 * Carbon::parse('2019-07-15')->isWeekday(); // true
364 * ```
365 */
366 public function isWeekday(): bool
367 {
368 return !$this->isWeekend();
369 }
370
371 /**
372 * Determines if the instance is a weekend day.
373 *
374 * @example
375 * ```
376 * Carbon::parse('2019-07-14')->isWeekend(); // true
377 * Carbon::parse('2019-07-15')->isWeekend(); // false
378 * ```
379 */
380 public function isWeekend(): bool
381 {
382 return \in_array(
383 $this->dayOfWeek,
384 $this->transmitFactory(static fn () => static::getWeekendDays()),
385 true,
386 );
387 }
388
389 /**
390 * Determines if the instance is yesterday.
391 *
392 * @example
393 * ```
394 * Carbon::yesterday()->isYesterday(); // true
395 * Carbon::tomorrow()->isYesterday(); // false
396 * ```
397 */
398 public function isYesterday(): bool
399 {
400 return $this->toDateString() === $this->transmitFactory(
401 fn () => static::yesterday($this->getTimezone())->toDateString(),
402 );
403 }
404
405 /**
406 * Determines if the instance is today.
407 *
408 * @example
409 * ```
410 * Carbon::today()->isToday(); // true
411 * Carbon::tomorrow()->isToday(); // false
412 * ```
413 */
414 public function isToday(): bool
415 {
416 return $this->toDateString() === $this->nowWithSameTz()->toDateString();
417 }
418
419 /**
420 * Determines if the instance is tomorrow.
421 *
422 * @example
423 * ```
424 * Carbon::tomorrow()->isTomorrow(); // true
425 * Carbon::yesterday()->isTomorrow(); // false
426 * ```
427 */
428 public function isTomorrow(): bool
429 {
430 return $this->toDateString() === $this->transmitFactory(
431 fn () => static::tomorrow($this->getTimezone())->toDateString(),
432 );
433 }
434
435 /**
436 * Determines if the instance is in the future, ie. greater (after) than now.
437 *
438 * @example
439 * ```
440 * Carbon::now()->addHours(5)->isFuture(); // true
441 * Carbon::now()->subHours(5)->isFuture(); // false
442 * ```
443 */
444 public function isFuture(): bool
445 {
446 return $this->greaterThan($this->nowWithSameTz());
447 }
448
449 /**
450 * Determines if the instance is in the past, ie. less (before) than now.
451 *
452 * @example
453 * ```
454 * Carbon::now()->subHours(5)->isPast(); // true
455 * Carbon::now()->addHours(5)->isPast(); // false
456 * ```
457 */
458 public function isPast(): bool
459 {
460 return $this->lessThan($this->nowWithSameTz());
461 }
462
463 /**
464 * Determines if the instance is now or in the future, ie. greater (after) than or equal to now.
465 *
466 * @example
467 * ```
468 * Carbon::now()->isNowOrFuture(); // true
469 * Carbon::now()->addHours(5)->isNowOrFuture(); // true
470 * Carbon::now()->subHours(5)->isNowOrFuture(); // false
471 * ```
472 */
473 public function isNowOrFuture(): bool
474 {
475 return $this->greaterThanOrEqualTo($this->nowWithSameTz());
476 }
477
478 /**
479 * Determines if the instance is now or in the past, ie. less (before) than or equal to now.
480 *
481 * @example
482 * ```
483 * Carbon::now()->isNowOrPast(); // true
484 * Carbon::now()->subHours(5)->isNowOrPast(); // true
485 * Carbon::now()->addHours(5)->isNowOrPast(); // false
486 * ```
487 */
488 public function isNowOrPast(): bool
489 {
490 return $this->lessThanOrEqualTo($this->nowWithSameTz());
491 }
492
493 /**
494 * Determines if the instance is a leap year.
495 *
496 * @example
497 * ```
498 * Carbon::parse('2020-01-01')->isLeapYear(); // true
499 * Carbon::parse('2019-01-01')->isLeapYear(); // false
500 * ```
501 */
502 public function isLeapYear(): bool
503 {
504 return $this->rawFormat('L') === '1';
505 }
506
507 /**
508 * Determines if the instance is a long year (using calendar year).
509 *
510 * ⚠️ This method completely ignores month and day to use the numeric year number,
511 * it's not correct if the exact date matters. For instance as `2019-12-30` is already
512 * in the first week of the 2020 year, if you want to know from this date if ISO week
513 * year 2020 is a long year, use `isLongIsoYear` instead.
514 *
515 * @example
516 * ```
517 * Carbon::create(2015)->isLongYear(); // true
518 * Carbon::create(2016)->isLongYear(); // false
519 * ```
520 *
521 * @see https://en.wikipedia.org/wiki/ISO_8601#Week_dates
522 */
523 public function isLongYear(): bool
524 {
525 return static::create($this->year, 12, 28, 0, 0, 0, $this->tz)->weekOfYear === static::WEEKS_PER_YEAR + 1;
526 }
527
528 /**
529 * Determines if the instance is a long year (using ISO 8601 year).
530 *
531 * @example
532 * ```
533 * Carbon::parse('2015-01-01')->isLongIsoYear(); // true
534 * Carbon::parse('2016-01-01')->isLongIsoYear(); // true
535 * Carbon::parse('2016-01-03')->isLongIsoYear(); // false
536 * Carbon::parse('2019-12-29')->isLongIsoYear(); // false
537 * Carbon::parse('2019-12-30')->isLongIsoYear(); // true
538 * ```
539 *
540 * @see https://en.wikipedia.org/wiki/ISO_8601#Week_dates
541 */
542 public function isLongIsoYear(): bool
543 {
544 return static::create($this->isoWeekYear, 12, 28, 0, 0, 0, $this->tz)->weekOfYear === 53;
545 }
546
547 /**
548 * Compares the formatted values of the two dates.
549 *
550 * @example
551 * ```
552 * Carbon::parse('2019-06-13')->isSameAs('Y-d', Carbon::parse('2019-12-13')); // true
553 * Carbon::parse('2019-06-13')->isSameAs('Y-d', Carbon::parse('2019-06-14')); // false
554 * ```
555 *
556 * @param string $format date formats to compare.
557 * @param DateTimeInterface|string $date instance to compare with or null to use current day.
558 */
559 public function isSameAs(string $format, DateTimeInterface|string $date): bool
560 {
561 return $this->rawFormat($format) === $this->resolveCarbon($date)->rawFormat($format);
562 }
563
564 /**
565 * Determines if the instance is in the current unit given.
566 *
567 * @example
568 * ```
569 * Carbon::parse('2019-01-13')->isSameUnit('year', Carbon::parse('2019-12-25')); // true
570 * Carbon::parse('2018-12-13')->isSameUnit('year', Carbon::parse('2019-12-25')); // false
571 * ```
572 *
573 * @param string $unit singular unit string
574 * @param DateTimeInterface|string $date instance to compare with or null to use current day.
575 *
576 * @throws BadComparisonUnitException
577 *
578 * @return bool
579 */
580 public function isSameUnit(string $unit, DateTimeInterface|string $date): bool
581 {
582 if ($unit === /* @call isSameUnit */ 'quarter') {
583 $other = $this->resolveCarbon($date);
584
585 return $other->year === $this->year && $other->quarter === $this->quarter;
586 }
587
588 $units = [
589 // @call isSameUnit
590 'year' => 'Y',
591 // @call isSameUnit
592 'month' => 'Y-n',
593 // @call isSameUnit
594 'week' => 'o-W',
595 // @call isSameUnit
596 'day' => 'Y-m-d',
597 // @call isSameUnit
598 'hour' => 'Y-m-d H',
599 // @call isSameUnit
600 'minute' => 'Y-m-d H:i',
601 // @call isSameUnit
602 'second' => 'Y-m-d H:i:s',
603 // @call isSameUnit
604 'milli' => 'Y-m-d H:i:s.v',
605 // @call isSameUnit
606 'millisecond' => 'Y-m-d H:i:s.v',
607 // @call isSameUnit
608 'micro' => 'Y-m-d H:i:s.u',
609 // @call isSameUnit
610 'microsecond' => 'Y-m-d H:i:s.u',
611 ];
612
613 if (isset($units[$unit])) {
614 return $this->isSameAs($units[$unit], $date);
615 }
616
617 if (isset($this->$unit)) {
618 return $this->resolveCarbon($date)->$unit === $this->$unit;
619 }
620
621 if ($this->isLocalStrictModeEnabled()) {
622 throw new BadComparisonUnitException($unit);
623 }
624
625 return false;
626 }
627
628 /**
629 * Determines if the instance is in the current unit given.
630 *
631 * @example
632 * ```
633 * Carbon::now()->isCurrentUnit('hour'); // true
634 * Carbon::now()->subHours(2)->isCurrentUnit('hour'); // false
635 * ```
636 *
637 * @param string $unit The unit to test.
638 *
639 * @throws BadMethodCallException
640 */
641 public function isCurrentUnit(string $unit): bool
642 {
643 return $this->{'isSame'.ucfirst($unit)}('now');
644 }
645
646 /**
647 * Checks if the passed in date is in the same quarter as the instance quarter (and year if needed).
648 *
649 * @example
650 * ```
651 * Carbon::parse('2019-01-12')->isSameQuarter(Carbon::parse('2019-03-01')); // true
652 * Carbon::parse('2019-01-12')->isSameQuarter(Carbon::parse('2019-04-01')); // false
653 * Carbon::parse('2019-01-12')->isSameQuarter(Carbon::parse('2018-03-01')); // false
654 * Carbon::parse('2019-01-12')->isSameQuarter(Carbon::parse('2018-03-01'), false); // true
655 * ```
656 *
657 * @param DateTimeInterface|string $date The instance to compare with or null to use current day.
658 * @param bool $ofSameYear Check if it is the same month in the same year.
659 *
660 * @return bool
661 */
662 public function isSameQuarter(DateTimeInterface|string $date, bool $ofSameYear = true): bool
663 {
664 $date = $this->resolveCarbon($date);
665
666 return $this->quarter === $date->quarter && (!$ofSameYear || $this->isSameYear($date));
667 }
668
669 /**
670 * Checks if the passed in date is in the same month as the instance´s month.
671 *
672 * @example
673 * ```
674 * Carbon::parse('2019-01-12')->isSameMonth(Carbon::parse('2019-01-01')); // true
675 * Carbon::parse('2019-01-12')->isSameMonth(Carbon::parse('2019-02-01')); // false
676 * Carbon::parse('2019-01-12')->isSameMonth(Carbon::parse('2018-01-01')); // false
677 * Carbon::parse('2019-01-12')->isSameMonth(Carbon::parse('2018-01-01'), false); // true
678 * ```
679 *
680 * @param DateTimeInterface|string $date The instance to compare with or null to use the current date.
681 * @param bool $ofSameYear Check if it is the same month in the same year.
682 *
683 * @return bool
684 */
685 public function isSameMonth(DateTimeInterface|string $date, bool $ofSameYear = true): bool
686 {
687 return $this->isSameAs($ofSameYear ? 'Y-m' : 'm', $date);
688 }
689
690 /**
691 * Checks if this day is a specific day of the week.
692 *
693 * @example
694 * ```
695 * Carbon::parse('2019-07-17')->isDayOfWeek(Carbon::WEDNESDAY); // true
696 * Carbon::parse('2019-07-17')->isDayOfWeek(Carbon::FRIDAY); // false
697 * Carbon::parse('2019-07-17')->isDayOfWeek('Wednesday'); // true
698 * Carbon::parse('2019-07-17')->isDayOfWeek('Friday'); // false
699 * ```
700 *
701 * @param int|string $dayOfWeek
702 *
703 * @return bool
704 */
705 public function isDayOfWeek($dayOfWeek): bool
706 {
707 if (\is_string($dayOfWeek) && \defined($constant = static::class.'::'.strtoupper($dayOfWeek))) {
708 $dayOfWeek = \constant($constant);
709 }
710
711 return $this->dayOfWeek === $dayOfWeek;
712 }
713
714 /**
715 * Check if its the birthday. Compares the date/month values of the two dates.
716 *
717 * @example
718 * ```
719 * Carbon::now()->subYears(5)->isBirthday(); // true
720 * Carbon::now()->subYears(5)->subDay()->isBirthday(); // false
721 * Carbon::parse('2019-06-05')->isBirthday(Carbon::parse('2001-06-05')); // true
722 * Carbon::parse('2019-06-05')->isBirthday(Carbon::parse('2001-06-06')); // false
723 * ```
724 *
725 * @param DateTimeInterface|string|null $date The instance to compare with or null to use current day.
726 *
727 * @return bool
728 */
729 public function isBirthday(DateTimeInterface|string|null $date = null): bool
730 {
731 return $this->isSameAs('md', $date ?? 'now');
732 }
733
734 /**
735 * Check if today is the last day of the Month
736 *
737 * @example
738 * ```
739 * Carbon::parse('2019-02-28')->isLastOfMonth(); // true
740 * Carbon::parse('2019-03-28')->isLastOfMonth(); // false
741 * Carbon::parse('2019-03-30')->isLastOfMonth(); // false
742 * Carbon::parse('2019-03-31')->isLastOfMonth(); // true
743 * Carbon::parse('2019-04-30')->isLastOfMonth(); // true
744 * ```
745 */
746 public function isLastOfMonth(): bool
747 {
748 return $this->day === $this->daysInMonth;
749 }
750
751 /**
752 * Check if the instance is start of a given unit (tolerating a given interval).
753 *
754 * @example
755 * ```
756 * // Check if a date-time is the first 15 minutes of the hour it's in
757 * Carbon::parse('2019-02-28 20:13:00')->isStartOfUnit(Unit::Hour, '15 minutes'); // true
758 * ```
759 */
760 public function isStartOfUnit(
761 Unit $unit,
762 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
763 mixed ...$params,
764 ): bool {
765 $interval ??= match ($unit) {
766 Unit::Day, Unit::Hour, Unit::Minute, Unit::Second, Unit::Millisecond, Unit::Microsecond => Unit::Microsecond,
767 default => Unit::Day,
768 };
769
770 $startOfUnit = $this->avoidMutation()->startOf($unit, ...$params);
771 $startOfUnitDateTime = $startOfUnit->rawFormat('Y-m-d H:i:s.u');
772 $maximumDateTime = $startOfUnit
773 ->add($interval instanceof Unit ? '1 '.$interval->value : $interval)
774 ->rawFormat('Y-m-d H:i:s.u');
775
776 if ($maximumDateTime < $startOfUnitDateTime) {
777 return false;
778 }
779
780 return $this->rawFormat('Y-m-d H:i:s.u') < $maximumDateTime;
781 }
782
783 /**
784 * Check if the instance is end of a given unit (tolerating a given interval).
785 *
786 * @example
787 * ```
788 * // Check if a date-time is the last 15 minutes of the hour it's in
789 * Carbon::parse('2019-02-28 20:13:00')->isEndOfUnit(Unit::Hour, '15 minutes'); // false
790 * ```
791 */
792 public function isEndOfUnit(
793 Unit $unit,
794 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
795 mixed ...$params,
796 ): bool {
797 $interval ??= match ($unit) {
798 Unit::Day, Unit::Hour, Unit::Minute, Unit::Second, Unit::Millisecond, Unit::Microsecond => Unit::Microsecond,
799 default => Unit::Day,
800 };
801
802 $endOfUnit = $this->avoidMutation()->endOf($unit, ...$params);
803 $endOfUnitDateTime = $endOfUnit->rawFormat('Y-m-d H:i:s.u');
804 $minimumDateTime = $endOfUnit
805 ->sub($interval instanceof Unit ? '1 '.$interval->value : $interval)
806 ->rawFormat('Y-m-d H:i:s.u');
807
808 if ($minimumDateTime > $endOfUnitDateTime) {
809 return false;
810 }
811
812 return $this->rawFormat('Y-m-d H:i:s.u') > $minimumDateTime;
813 }
814
815 /**
816 * Determines if the instance is start of millisecond (first microsecond by default but interval can be customized).
817 */
818 public function isStartOfMillisecond(
819 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
820 ): bool {
821 return $this->isStartOfUnit(Unit::Millisecond, $interval);
822 }
823
824 /**
825 * Determines if the instance is end of millisecond (last microsecond by default but interval can be customized).
826 */
827 public function isEndOfMillisecond(
828 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
829 ): bool {
830 return $this->isEndOfUnit(Unit::Millisecond, $interval);
831 }
832
833 /**
834 * Determines if the instance is start of second (first microsecond by default but interval can be customized).
835 */
836 public function isStartOfSecond(
837 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
838 ): bool {
839 return $this->isStartOfUnit(Unit::Second, $interval);
840 }
841
842 /**
843 * Determines if the instance is end of second (last microsecond by default but interval can be customized).
844 */
845 public function isEndOfSecond(
846 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
847 ): bool {
848 return $this->isEndOfUnit(Unit::Second, $interval);
849 }
850
851 /**
852 * Determines if the instance is start of minute (first microsecond by default but interval can be customized).
853 */
854 public function isStartOfMinute(
855 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
856 ): bool {
857 return $this->isStartOfUnit(Unit::Minute, $interval);
858 }
859
860 /**
861 * Determines if the instance is end of minute (last microsecond by default but interval can be customized).
862 */
863 public function isEndOfMinute(
864 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
865 ): bool {
866 return $this->isEndOfUnit(Unit::Minute, $interval);
867 }
868
869 /**
870 * Determines if the instance is start of hour (first microsecond by default but interval can be customized).
871 */
872 public function isStartOfHour(
873 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
874 ): bool {
875 return $this->isStartOfUnit(Unit::Hour, $interval);
876 }
877
878 /**
879 * Determines if the instance is end of hour (last microsecond by default but interval can be customized).
880 */
881 public function isEndOfHour(
882 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
883 ): bool {
884 return $this->isEndOfUnit(Unit::Hour, $interval);
885 }
886
887 /**
888 * Check if the instance is start of day / midnight.
889 *
890 * @example
891 * ```
892 * Carbon::parse('2019-02-28 00:00:00')->isStartOfDay(); // true
893 * Carbon::parse('2019-02-28 00:00:00.999999')->isStartOfDay(); // true
894 * Carbon::parse('2019-02-28 00:00:01')->isStartOfDay(); // false
895 * Carbon::parse('2019-02-28 00:00:00.000000')->isStartOfDay(true); // true
896 * Carbon::parse('2019-02-28 00:00:00.000012')->isStartOfDay(true); // false
897 * ```
898 *
899 * @param bool $checkMicroseconds check time at microseconds precision
900 * @param Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval if an interval is specified it will be used as precision
901 * for instance with "15 minutes", it checks if current date-time
902 * is in the last 15 minutes of the day, with Unit::Hour, it
903 * checks if it's in the last hour of the day.
904 */
905 public function isStartOfDay(
906 Unit|DateInterval|Closure|CarbonConverterInterface|string|bool $checkMicroseconds = false,
907 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
908 ): bool {
909 if ($checkMicroseconds === true) {
910 @trigger_error(
911 "Since 3.8.0, it's deprecated to use \$checkMicroseconds.\n".
912 "It will be removed in 4.0.0.\n".
913 "Instead, you should use either isStartOfDay(interval: Unit::Microsecond) or isStartOfDay(interval: Unit::Second)\n".
914 'And you can now use any custom interval as precision, such as isStartOfDay(interval: "15 minutes")',
915 \E_USER_DEPRECATED,
916 );
917 }
918
919 if ($interval === null && !\is_bool($checkMicroseconds)) {
920 $interval = $checkMicroseconds;
921 }
922
923 if ($interval !== null) {
924 if ($interval instanceof Unit) {
925 $interval = '1 '.$interval->value;
926 }
927
928 $date = $this->rawFormat('Y-m-d');
929 $time = $this->rawFormat('H:i:s.u');
930 $maximum = $this->avoidMutation()->startOfDay()->add($interval);
931 $maximumDate = $maximum->rawFormat('Y-m-d');
932
933 if ($date === $maximumDate) {
934 return $time < $maximum->rawFormat('H:i:s.u');
935 }
936
937 return $maximumDate > $date;
938 }
939
940 /* @var CarbonInterface $this */
941 return $checkMicroseconds
942 ? $this->rawFormat('H:i:s.u') === '00:00:00.000000'
943 : $this->rawFormat('H:i:s') === '00:00:00';
944 }
945
946 /**
947 * Check if the instance is end of day.
948 *
949 * @example
950 * ```
951 * Carbon::parse('2019-02-28 23:59:59.999999')->isEndOfDay(); // true
952 * Carbon::parse('2019-02-28 23:59:59.123456')->isEndOfDay(); // true
953 * Carbon::parse('2019-02-28 23:59:59')->isEndOfDay(); // true
954 * Carbon::parse('2019-02-28 23:59:58.999999')->isEndOfDay(); // false
955 * Carbon::parse('2019-02-28 23:59:59.999999')->isEndOfDay(true); // true
956 * Carbon::parse('2019-02-28 23:59:59.123456')->isEndOfDay(true); // false
957 * Carbon::parse('2019-02-28 23:59:59')->isEndOfDay(true); // false
958 * ```
959 *
960 * @param bool $checkMicroseconds check time at microseconds precision
961 * @param Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval if an interval is specified it will be used as precision
962 * for instance with "15 minutes", it checks if current date-time
963 * is in the last 15 minutes of the day, with Unit::Hour, it
964 * checks if it's in the last hour of the day.
965 */
966 public function isEndOfDay(
967 Unit|DateInterval|Closure|CarbonConverterInterface|string|bool $checkMicroseconds = false,
968 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
969 ): bool {
970 if ($checkMicroseconds === true) {
971 @trigger_error(
972 "Since 3.8.0, it's deprecated to use \$checkMicroseconds.\n".
973 "It will be removed in 4.0.0.\n".
974 "Instead, you should use either isEndOfDay(interval: Unit::Microsecond) or isEndOfDay(interval: Unit::Second)\n".
975 'And you can now use any custom interval as precision, such as isEndOfDay(interval: "15 minutes")',
976 \E_USER_DEPRECATED,
977 );
978 }
979
980 if ($interval === null && !\is_bool($checkMicroseconds)) {
981 $interval = $checkMicroseconds;
982 }
983
984 if ($interval !== null) {
985 $date = $this->rawFormat('Y-m-d');
986 $time = $this->rawFormat('H:i:s.u');
987 $minimum = $this->avoidMutation()
988 ->endOfDay()
989 ->sub($interval instanceof Unit ? '1 '.$interval->value : $interval);
990 $minimumDate = $minimum->rawFormat('Y-m-d');
991
992 if ($date === $minimumDate) {
993 return $time > $minimum->rawFormat('H:i:s.u');
994 }
995
996 return $minimumDate < $date;
997 }
998
999 /* @var CarbonInterface $this */
1000 return $checkMicroseconds
1001 ? $this->rawFormat('H:i:s.u') === '23:59:59.999999'
1002 : $this->rawFormat('H:i:s') === '23:59:59';
1003 }
1004
1005 /**
1006 * Determines if the instance is start of week (first day by default but interval can be customized).
1007 *
1008 * @example
1009 * ```
1010 * Carbon::parse('2024-08-31')->startOfWeek()->isStartOfWeek(); // true
1011 * Carbon::parse('2024-08-31')->isStartOfWeek(); // false
1012 * ```
1013 */
1014 public function isStartOfWeek(
1015 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1016 WeekDay|int|null $weekStartsAt = null,
1017 ): bool {
1018 return $this->isStartOfUnit(Unit::Week, $interval, $weekStartsAt);
1019 }
1020
1021 /**
1022 * Determines if the instance is end of week (last day by default but interval can be customized).
1023 *
1024 * @example
1025 * ```
1026 * Carbon::parse('2024-08-31')->endOfWeek()->isEndOfWeek(); // true
1027 * Carbon::parse('2024-08-31')->isEndOfWeek(); // false
1028 * ```
1029 */
1030 public function isEndOfWeek(
1031 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1032 WeekDay|int|null $weekEndsAt = null,
1033 ): bool {
1034 return $this->isEndOfUnit(Unit::Week, $interval, $weekEndsAt);
1035 }
1036
1037 /**
1038 * Determines if the instance is start of month (first day by default but interval can be customized).
1039 */
1040 public function isStartOfMonth(
1041 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1042 ): bool {
1043 return $this->isStartOfUnit(Unit::Month, $interval);
1044 }
1045
1046 /**
1047 * Determines if the instance is end of month (last day by default but interval can be customized).
1048 */
1049 public function isEndOfMonth(
1050 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1051 ): bool {
1052 return $this->isEndOfUnit(Unit::Month, $interval);
1053 }
1054
1055 /**
1056 * Determines if the instance is start of quarter (first day by default but interval can be customized).
1057 */
1058 public function isStartOfQuarter(
1059 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1060 ): bool {
1061 return $this->isStartOfUnit(Unit::Quarter, $interval);
1062 }
1063
1064 /**
1065 * Determines if the instance is end of quarter (last day by default but interval can be customized).
1066 */
1067 public function isEndOfQuarter(
1068 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1069 ): bool {
1070 return $this->isEndOfUnit(Unit::Quarter, $interval);
1071 }
1072
1073 /**
1074 * Determines if the instance is start of year (first day by default but interval can be customized).
1075 */
1076 public function isStartOfYear(
1077 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1078 ): bool {
1079 return $this->isStartOfUnit(Unit::Year, $interval);
1080 }
1081
1082 /**
1083 * Determines if the instance is end of year (last day by default but interval can be customized).
1084 */
1085 public function isEndOfYear(
1086 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1087 ): bool {
1088 return $this->isEndOfUnit(Unit::Year, $interval);
1089 }
1090
1091 /**
1092 * Determines if the instance is start of decade (first day by default but interval can be customized).
1093 */
1094 public function isStartOfDecade(
1095 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1096 ): bool {
1097 return $this->isStartOfUnit(Unit::Decade, $interval);
1098 }
1099
1100 /**
1101 * Determines if the instance is end of decade (last day by default but interval can be customized).
1102 */
1103 public function isEndOfDecade(
1104 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1105 ): bool {
1106 return $this->isEndOfUnit(Unit::Decade, $interval);
1107 }
1108
1109 /**
1110 * Determines if the instance is start of century (first day by default but interval can be customized).
1111 */
1112 public function isStartOfCentury(
1113 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1114 ): bool {
1115 return $this->isStartOfUnit(Unit::Century, $interval);
1116 }
1117
1118 /**
1119 * Determines if the instance is end of century (last day by default but interval can be customized).
1120 */
1121 public function isEndOfCentury(
1122 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1123 ): bool {
1124 return $this->isEndOfUnit(Unit::Century, $interval);
1125 }
1126
1127 /**
1128 * Determines if the instance is start of millennium (first day by default but interval can be customized).
1129 */
1130 public function isStartOfMillennium(
1131 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1132 ): bool {
1133 return $this->isStartOfUnit(Unit::Millennium, $interval);
1134 }
1135
1136 /**
1137 * Determines if the instance is end of millennium (last day by default but interval can be customized).
1138 */
1139 public function isEndOfMillennium(
1140 Unit|DateInterval|Closure|CarbonConverterInterface|string|null $interval = null,
1141 ): bool {
1142 return $this->isEndOfUnit(Unit::Millennium, $interval);
1143 }
1144
1145 /**
1146 * Check if the instance is start of day / midnight.
1147 *
1148 * @example
1149 * ```
1150 * Carbon::parse('2019-02-28 00:00:00')->isMidnight(); // true
1151 * Carbon::parse('2019-02-28 00:00:00.999999')->isMidnight(); // true
1152 * Carbon::parse('2019-02-28 00:00:01')->isMidnight(); // false
1153 * ```
1154 */
1155 public function isMidnight(): bool
1156 {
1157 return $this->isStartOfDay();
1158 }
1159
1160 /**
1161 * Check if the instance is midday.
1162 *
1163 * @example
1164 * ```
1165 * Carbon::parse('2019-02-28 11:59:59.999999')->isMidday(); // false
1166 * Carbon::parse('2019-02-28 12:00:00')->isMidday(); // true
1167 * Carbon::parse('2019-02-28 12:00:00.999999')->isMidday(); // true
1168 * Carbon::parse('2019-02-28 12:00:01')->isMidday(); // false
1169 * ```
1170 */
1171 public function isMidday(): bool
1172 {
1173 /* @var CarbonInterface $this */
1174 return $this->rawFormat('G:i:s') === static::$midDayAt.':00:00';
1175 }
1176
1177 /**
1178 * Checks if the (date)time string is in a given format.
1179 *
1180 * @example
1181 * ```
1182 * Carbon::hasFormat('11:12:45', 'h:i:s'); // true
1183 * Carbon::hasFormat('13:12:45', 'h:i:s'); // false
1184 * ```
1185 */
1186 public static function hasFormat(string $date, string $format): bool
1187 {
1188 return FactoryImmutable::getInstance()->hasFormat($date, $format);
1189 }
1190
1191 /**
1192 * Checks if the (date)time string is in a given format.
1193 *
1194 * @example
1195 * ```
1196 * Carbon::hasFormatWithModifiers('31/08/2015', 'd#m#Y'); // true
1197 * Carbon::hasFormatWithModifiers('31/08/2015', 'm#d#Y'); // false
1198 * ```
1199 *
1200 * @param string $date
1201 * @param string $format
1202 *
1203 * @return bool
1204 */
1205 public static function hasFormatWithModifiers(?string $date, string $format): bool
1206 {
1207 return FactoryImmutable::getInstance()->hasFormatWithModifiers($date, $format);
1208 }
1209
1210 /**
1211 * Checks if the (date)time string is in a given format and valid to create a
1212 * new instance.
1213 *
1214 * @example
1215 * ```
1216 * Carbon::canBeCreatedFromFormat('11:12:45', 'h:i:s'); // true
1217 * Carbon::canBeCreatedFromFormat('13:12:45', 'h:i:s'); // false
1218 * ```
1219 */
1220 public static function canBeCreatedFromFormat(?string $date, string $format): bool
1221 {
1222 if ($date === null) {
1223 return false;
1224 }
1225
1226 try {
1227 // Try to create a DateTime object. Throws an InvalidArgumentException if the provided time string
1228 // doesn't match the format in any way.
1229 if (!static::rawCreateFromFormat($format, $date)) {
1230 return false;
1231 }
1232 } catch (InvalidArgumentException) {
1233 return false;
1234 }
1235
1236 return static::hasFormatWithModifiers($date, $format);
1237 }
1238
1239 /**
1240 * Returns true if the current date matches the given string.
1241 *
1242 * @example
1243 * ```
1244 * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2019')); // true
1245 * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2018')); // false
1246 * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2019-06')); // true
1247 * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('06-02')); // true
1248 * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-02')); // true
1249 * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('Sunday')); // true
1250 * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('June')); // true
1251 * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12:23')); // true
1252 * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12:23:45')); // true
1253 * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12:23:00')); // false
1254 * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12h')); // true
1255 * var_dump(Carbon::parse('2019-06-02 15:23:45')->is('3pm')); // true
1256 * var_dump(Carbon::parse('2019-06-02 15:23:45')->is('3am')); // false
1257 * ```
1258 *
1259 * @param string $tester day name, month name, hour, date, etc. as string
1260 */
1261 public function is(WeekDay|Month|string $tester): bool
1262 {
1263 if ($tester instanceof BackedEnum) {
1264 $tester = $tester->name;
1265 }
1266
1267 $tester = trim($tester);
1268
1269 if (preg_match('/^\d+$/', $tester)) {
1270 return $this->year === (int) $tester;
1271 }
1272
1273 if (preg_match('/^(?:Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)$/i', $tester)) {
1274 return $this->isSameMonth(
1275 $this->transmitFactory(static fn () => static::parse("$tester 1st")),
1276 false,
1277 );
1278 }
1279
1280 if (preg_match('/^\d{3,}-\d{1,2}$/', $tester)) {
1281 return $this->isSameMonth(
1282 $this->transmitFactory(static fn () => static::parse($tester)),
1283 );
1284 }
1285
1286 if (preg_match('/^(\d{1,2})-(\d{1,2})$/', $tester, $match)) {
1287 return $this->month === (int) $match[1] && $this->day === (int) $match[2];
1288 }
1289
1290 $modifier = preg_replace('/(\d)h$/i', '$1:00', $tester);
1291
1292 /* @var CarbonInterface $max */
1293 $median = $this->transmitFactory(static fn () => static::parse('5555-06-15 12:30:30.555555'))
1294 ->modify($modifier);
1295 $current = $this->avoidMutation();
1296 /* @var CarbonInterface $other */
1297 $other = $this->avoidMutation()->modify($modifier);
1298
1299 if ($current->eq($other)) {
1300 return true;
1301 }
1302
1303 if (preg_match('/\d:\d{1,2}:\d{1,2}$/', $tester)) {
1304 return $current->startOfSecond()->eq($other);
1305 }
1306
1307 if (preg_match('/\d:\d{1,2}$/', $tester)) {
1308 return $current->startOfMinute()->eq($other);
1309 }
1310
1311 if (preg_match('/\d(?:h|am|pm)$/', $tester)) {
1312 return $current->startOfHour()->eq($other);
1313 }
1314
1315 if (preg_match(
1316 '/^(?:january|february|march|april|may|june|july|august|september|october|november|december)(?:\s+\d+)?$/i',
1317 $tester,
1318 )) {
1319 return $current->startOfMonth()->eq($other->startOfMonth());
1320 }
1321
1322 $units = [
1323 'month' => [1, 'year'],
1324 'day' => [1, 'month'],
1325 'hour' => [0, 'day'],
1326 'minute' => [0, 'hour'],
1327 'second' => [0, 'minute'],
1328 'microsecond' => [0, 'second'],
1329 ];
1330
1331 foreach ($units as $unit => [$minimum, $startUnit]) {
1332 if ($minimum === $median->$unit) {
1333 $current = $current->startOf($startUnit);
1334
1335 break;
1336 }
1337 }
1338
1339 return $current->eq($other);
1340 }
1341
1342 /**
1343 * Returns true if the date was created using CarbonImmutable::startOfTime()
1344 *
1345 * @return bool
1346 */
1347 public function isStartOfTime(): bool
1348 {
1349 return $this->startOfTime ?? false;
1350 }
1351
1352 /**
1353 * Returns true if the date was created using CarbonImmutable::endOfTime()
1354 *
1355 * @return bool
1356 */
1357 public function isEndOfTime(): bool
1358 {
1359 return $this->endOfTime ?? false;
1360 }
1361 }