1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6: */
7:
8: namespace Nette;
9:
10: use Nette;
11:
12:
13: /**
14: * DateTime with serialization and timestamp support for PHP 5.2.
15: *
16: * @author David Grudl
17: */
18: class DateTime extends \DateTime
19: {
20: /** minute in seconds */
21: const MINUTE = 60;
22:
23: /** hour in seconds */
24: const HOUR = 3600;
25:
26: /** day in seconds */
27: const DAY = 86400;
28:
29: /** week in seconds */
30: const WEEK = 604800;
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|\DateTime
42: * @return DateTime
43: */
44: public static function from($time)
45: {
46: if ($time instanceof \DateTime || $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: $tmp = new static('@' . $time);
54: $tmp->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
55: return $tmp;
56:
57: } else { // textual or NULL
58: return new static($time);
59: }
60: }
61:
62:
63: /**
64: * @return string
65: */
66: public function __toString()
67: {
68: return $this->format('Y-m-d H:i:s');
69: }
70:
71:
72: /**
73: * @param string
74: * @return self
75: */
76: public function modifyClone($modify = '')
77: {
78: $dolly = clone $this;
79: return $modify ? $dolly->modify($modify) : $dolly;
80: }
81:
82:
83: /**
84: * @param int
85: * @return self
86: */
87: public function setTimestamp($timestamp)
88: {
89: $zone = PHP_VERSION_ID === 50206 ? new \DateTimeZone($this->getTimezone()->getName()) : $this->getTimezone();
90: $this->__construct('@' . $timestamp);
91: $this->setTimeZone($zone);
92: return $this;
93: }
94:
95:
96: /**
97: * @return int|string
98: */
99: public function getTimestamp()
100: {
101: $ts = $this->format('U');
102: return is_float($tmp = $ts * 1) ? $ts : $tmp;
103: }
104:
105:
106: }
107: