PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / utils / src / Utils / DateTime.php

DateTime.php in Packeta trunk, at deps/nette/utils/src/Utils/DateTime.php

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