DateTime.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (https://nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. declare(strict_types=1);
  7. namespace Nette\Utils;
  8. use Nette;
  9. /**
  10. * DateTime.
  11. */
  12. class DateTime extends \DateTime implements \JsonSerializable
  13. {
  14. use Nette\SmartObject;
  15. /** minute in seconds */
  16. public const MINUTE = 60;
  17. /** hour in seconds */
  18. public const HOUR = 60 * self::MINUTE;
  19. /** day in seconds */
  20. public const DAY = 24 * self::HOUR;
  21. /** week in seconds */
  22. public const WEEK = 7 * self::DAY;
  23. /** average month in seconds */
  24. public const MONTH = 2629800;
  25. /** average year in seconds */
  26. public const YEAR = 31557600;
  27. /**
  28. * Creates a DateTime object from a string, UNIX timestamp, or other DateTimeInterface object.
  29. * @param string|int|\DateTimeInterface $time
  30. * @return static
  31. * @throws \Exception if the date and time are not valid.
  32. */
  33. public static function from($time)
  34. {
  35. if ($time instanceof \DateTimeInterface) {
  36. return new static($time->format('Y-m-d H:i:s.u'), $time->getTimezone());
  37. } elseif (is_numeric($time)) {
  38. if ($time <= self::YEAR) {
  39. $time += time();
  40. }
  41. return (new static('@' . $time))->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  42. } else { // textual or null
  43. return new static((string) $time);
  44. }
  45. }
  46. /**
  47. * Creates DateTime object.
  48. * @return static
  49. * @throws Nette\InvalidArgumentException if the date and time are not valid.
  50. */
  51. public static function fromParts(
  52. int $year,
  53. int $month,
  54. int $day,
  55. int $hour = 0,
  56. int $minute = 0,
  57. float $second = 0.0
  58. ) {
  59. $s = sprintf('%04d-%02d-%02d %02d:%02d:%02.5F', $year, $month, $day, $hour, $minute, $second);
  60. if (
  61. !checkdate($month, $day, $year)
  62. || $hour < 0
  63. || $hour > 23
  64. || $minute < 0
  65. || $minute > 59
  66. || $second < 0
  67. || $second >= 60
  68. ) {
  69. throw new Nette\InvalidArgumentException("Invalid date '$s'");
  70. }
  71. return new static($s);
  72. }
  73. /**
  74. * Returns new DateTime object formatted according to the specified format.
  75. * @param string $format The format the $time parameter should be in
  76. * @param string $time
  77. * @param string|\DateTimeZone $timezone (default timezone is used if null is passed)
  78. * @return static|false
  79. */
  80. #[\ReturnTypeWillChange]
  81. public static function createFromFormat($format, $time, $timezone = null)
  82. {
  83. if ($timezone === null) {
  84. $timezone = new \DateTimeZone(date_default_timezone_get());
  85. } elseif (is_string($timezone)) {
  86. $timezone = new \DateTimeZone($timezone);
  87. } elseif (!$timezone instanceof \DateTimeZone) {
  88. throw new Nette\InvalidArgumentException('Invalid timezone given');
  89. }
  90. $date = parent::createFromFormat($format, $time, $timezone);
  91. return $date ? static::from($date) : false;
  92. }
  93. /**
  94. * Returns JSON representation in ISO 8601 (used by JavaScript).
  95. */
  96. public function jsonSerialize(): string
  97. {
  98. return $this->format('c');
  99. }
  100. /**
  101. * Returns the date and time in the format 'Y-m-d H:i:s'.
  102. */
  103. public function __toString(): string
  104. {
  105. return $this->format('Y-m-d H:i:s');
  106. }
  107. /**
  108. * Creates a copy with a modified time.
  109. * @return static
  110. */
  111. public function modifyClone(string $modify = '')
  112. {
  113. $dolly = clone $this;
  114. return $modify ? $dolly->modify($modify) : $dolly;
  115. }
  116. }