| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
trait Comparison |
| 45 |
{ |
| 46 |
protected bool $endOfTime = false; |
| 47 |
|
| 48 |
protected bool $startOfTime = false; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function eq(DateTimeInterface|string $date): bool |
| 63 |
{ |
| 64 |
return $this->equalTo($date); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function equalTo(DateTimeInterface|string $date): bool |
| 78 |
{ |
| 79 |
return $this == $this->resolveCarbon($date); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function ne(DateTimeInterface|string $date): bool |
| 95 |
{ |
| 96 |
return $this->notEqualTo($date); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function notEqualTo(DateTimeInterface|string $date): bool |
| 110 |
{ |
| 111 |
return !$this->equalTo($date); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function gt(DateTimeInterface|string $date): bool |
| 127 |
{ |
| 128 |
return $this->greaterThan($date); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function greaterThan(DateTimeInterface|string $date): bool |
| 142 |
{ |
| 143 |
return $this > $this->resolveCarbon($date); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function isAfter(DateTimeInterface|string $date): bool |
| 159 |
{ |
| 160 |
return $this->greaterThan($date); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function gte(DateTimeInterface|string $date): bool |
| 176 |
{ |
| 177 |
return $this->greaterThanOrEqualTo($date); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function greaterThanOrEqualTo(DateTimeInterface|string $date): bool |
| 191 |
{ |
| 192 |
return $this >= $this->resolveCarbon($date); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public function lt(DateTimeInterface|string $date): bool |
| 208 |
{ |
| 209 |
return $this->lessThan($date); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function lessThan(DateTimeInterface|string $date): bool |
| 223 |
{ |
| 224 |
return $this < $this->resolveCarbon($date); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public function isBefore(DateTimeInterface|string $date): bool |
| 240 |
{ |
| 241 |
return $this->lessThan($date); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public function lte(DateTimeInterface|string $date): bool |
| 257 |
{ |
| 258 |
return $this->lessThanOrEqualTo($date); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function lessThanOrEqualTo(DateTimeInterface|string $date): bool |
| 272 |
{ |
| 273 |
return $this <= $this->resolveCarbon($date); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 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 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 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 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 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 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 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 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public function isWeekday(): bool |
| 367 |
{ |
| 368 |
return !$this->isWeekend(); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 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 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 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 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public function isToday(): bool |
| 415 |
{ |
| 416 |
return $this->toDateString() === $this->nowWithSameTz()->toDateString(); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 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 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public function isFuture(): bool |
| 445 |
{ |
| 446 |
return $this->greaterThan($this->nowWithSameTz()); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
public function isPast(): bool |
| 459 |
{ |
| 460 |
return $this->lessThan($this->nowWithSameTz()); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
public function isNowOrFuture(): bool |
| 474 |
{ |
| 475 |
return $this->greaterThanOrEqualTo($this->nowWithSameTz()); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
public function isNowOrPast(): bool |
| 489 |
{ |
| 490 |
return $this->lessThanOrEqualTo($this->nowWithSameTz()); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
public function isLeapYear(): bool |
| 503 |
{ |
| 504 |
return $this->rawFormat('L') === '1'; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
ber, |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 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 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 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 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 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 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
public function isSameUnit(string $unit, DateTimeInterface|string $date): bool |
| 581 |
{ |
| 582 |
if ($unit === 'quarter') { |
| 583 |
$other = $this->resolveCarbon($date); |
| 584 |
|
| 585 |
return $other->year === $this->year && $other->quarter === $this->quarter; |
| 586 |
} |
| 587 |
|
| 588 |
$units = [ |
| 589 |
|
| 590 |
'year' => 'Y', |
| 591 |
|
| 592 |
'month' => 'Y-n', |
| 593 |
|
| 594 |
'week' => 'o-W', |
| 595 |
|
| 596 |
'day' => 'Y-m-d', |
| 597 |
|
| 598 |
'hour' => 'Y-m-d H', |
| 599 |
|
| 600 |
'minute' => 'Y-m-d H:i', |
| 601 |
|
| 602 |
'second' => 'Y-m-d H:i:s', |
| 603 |
|
| 604 |
'milli' => 'Y-m-d H:i:s.v', |
| 605 |
|
| 606 |
'millisecond' => 'Y-m-d H:i:s.v', |
| 607 |
|
| 608 |
'micro' => 'Y-m-d H:i:s.u', |
| 609 |
|
| 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 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
public function isCurrentUnit(string $unit): bool |
| 642 |
{ |
| 643 |
return $this->{'isSame'.ucfirst($unit)}('now'); |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 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 |
. |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 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 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 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 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
public function isBirthday(DateTimeInterface|string|null $date = null): bool |
| 730 |
{ |
| 731 |
return $this->isSameAs('md', $date ?? 'now'); |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
public function isLastOfMonth(): bool |
| 747 |
{ |
| 748 |
return $this->day === $this->daysInMonth; |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 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 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 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 |
|
| 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 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 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 |
|
| 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 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 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 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
public function isMidnight(): bool |
| 1156 |
{ |
| 1157 |
return $this->isStartOfDay(); |
| 1158 |
} |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
public function isMidday(): bool |
| 1172 |
{ |
| 1173 |
|
| 1174 |
return $this->rawFormat('G:i:s') === static::$midDayAt.':00:00'; |
| 1175 |
} |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
public static function hasFormat(string $date, string $format): bool |
| 1187 |
{ |
| 1188 |
return FactoryImmutable::getInstance()->hasFormat($date, $format); |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
public static function hasFormatWithModifiers(?string $date, string $format): bool |
| 1206 |
{ |
| 1207 |
return FactoryImmutable::getInstance()->hasFormatWithModifiers($date, $format); |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 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 |
|
| 1228 |
|
| 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 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 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 |
|
| 1293 |
$median = $this->transmitFactory(static fn () => static::parse('5555-06-15 12:30:30.555555')) |
| 1294 |
->modify($modifier); |
| 1295 |
$current = $this->avoidMutation(); |
| 1296 |
|
| 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 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
public function isStartOfTime(): bool |
| 1348 |
{ |
| 1349 |
return $this->startOfTime ?? false; |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
public function isEndOfTime(): bool |
| 1358 |
{ |
| 1359 |
return $this->endOfTime ?? false; |
| 1360 |
} |
| 1361 |
} |