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:
8: namespace Nette\Utils;
9:
10: use Nette;
11:
12:
13: /**
14: * DateTime.
15: */
16: class DateTime extends \DateTime implements \JsonSerializable
17: {
18: use Nette\SmartObject;
19:
20: /** minute in seconds */
21: const MINUTE = 60;
22:
23: /** hour in seconds */
24: const HOUR = 60 * self::MINUTE;
25:
26: /** day in seconds */
27: const DAY = 24 * self::HOUR;
28:
29: /** week in seconds */
30: const WEEK = 7 * self::DAY;
31:
32: /** average month in seconds */
33: const MONTH = 2629800;
34:
35: /** average year in seconds */
36: const YEAR = 31557600;
37:
38:
39: /**
40: * DateTime object factory.
41: * @param string|int|\DateTimeInterface
42: * @return self
43: */
44: public static function from($time)
45: {
46: if ($time instanceof \DateTimeInterface) {
47: return new static($time->format('Y-m-d H:i:s'), $time->getTimezone());
48:
49: } elseif (is_numeric($time)) {
50: if ($time <= self::YEAR) {
51: $time += time();
52: }
53: return (new static('@' . $time))->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
54:
55: } else { // textual or NULL
56: return new static($time);
57: }
58: }
59:
60:
61: /**
62: * @return string
63: */
64: public function __toString()
65: {
66: return $this->format('Y-m-d H:i:s');
67: }
68:
69:
70: /**
71: * @param string
72: * @return self
73: */
74: public function modifyClone($modify = '')
75: {
76: $dolly = clone $this;
77: return $modify ? $dolly->modify($modify) : $dolly;
78: }
79:
80:
81: /**
82: * @param int
83: * @return self
84: */
85: public function setTimestamp($timestamp)
86: {
87: $zone = $this->getTimezone();
88: $this->__construct('@' . $timestamp);
89: return $this->setTimeZone($zone);
90: }
91:
92:
93: /**
94: * @return int|string
95: */
96: public function getTimestamp()
97: {
98: $ts = $this->format('U');
99: return is_float($tmp = $ts * 1) ? $ts : $tmp;
100: }
101:
102:
103: /**
104: * Returns new DateTime object formatted according to the specified format.
105: * @param string The format the $time parameter should be in
106: * @param string String representing the time
107: * @param string|\DateTimeZone desired timezone (default timezone is used if NULL is passed)
108: * @return self|FALSE
109: */
110: public static function createFromFormat($format, $time, $timezone = NULL)
111: {
112: if ($timezone === NULL) {
113: $timezone = new \DateTimeZone(date_default_timezone_get());
114:
115: } elseif (is_string($timezone)) {
116: $timezone = new \DateTimeZone($timezone);
117:
118: } elseif (!$timezone instanceof \DateTimeZone) {
119: throw new Nette\InvalidArgumentException('Invalid timezone given');
120: }
121:
122: $date = parent::createFromFormat($format, $time, $timezone);
123: return $date ? static::from($date) : FALSE;
124: }
125:
126:
127: /**
128: * Returns JSON representation in ISO 8601 (used by JavaScript).
129: * @return string
130: */
131: public function jsonSerialize()
132: {
133: return $this->format('c');
134: }
135:
136: }
137: