Carbon.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Illuminate\Support;
  3. use Carbon\Carbon as BaseCarbon;
  4. use Carbon\CarbonImmutable as BaseCarbonImmutable;
  5. use Illuminate\Support\Traits\Conditionable;
  6. use Illuminate\Support\Traits\Dumpable;
  7. use Ramsey\Uuid\Uuid;
  8. use Symfony\Component\Uid\Ulid;
  9. class Carbon extends BaseCarbon
  10. {
  11. use Conditionable, Dumpable;
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public static function setTestNow(mixed $testNow = null): void
  16. {
  17. BaseCarbon::setTestNow($testNow);
  18. BaseCarbonImmutable::setTestNow($testNow);
  19. }
  20. /**
  21. * Create a Carbon instance from a given ordered UUID or ULID.
  22. */
  23. public static function createFromId(Uuid|Ulid|string $id): static
  24. {
  25. if (is_string($id)) {
  26. $id = Ulid::isValid($id) ? Ulid::fromString($id) : Uuid::fromString($id);
  27. }
  28. return static::createFromInterface($id->getDateTime());
  29. }
  30. /**
  31. * Get the current date / time plus a given amount of time.
  32. */
  33. public function plus(
  34. int $years = 0,
  35. int $months = 0,
  36. int $weeks = 0,
  37. int $days = 0,
  38. int $hours = 0,
  39. int $minutes = 0,
  40. int $seconds = 0,
  41. int $microseconds = 0
  42. ): static {
  43. return $this->add("
  44. $years years $months months $weeks weeks $days days
  45. $hours hours $minutes minutes $seconds seconds $microseconds microseconds
  46. ");
  47. }
  48. /**
  49. * Get the current date / time minus a given amount of time.
  50. */
  51. public function minus(
  52. int $years = 0,
  53. int $months = 0,
  54. int $weeks = 0,
  55. int $days = 0,
  56. int $hours = 0,
  57. int $minutes = 0,
  58. int $seconds = 0,
  59. int $microseconds = 0
  60. ): static {
  61. return $this->sub("
  62. $years years $months months $weeks weeks $days days
  63. $hours hours $minutes minutes $seconds seconds $microseconds microseconds
  64. ");
  65. }
  66. }