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;
9:
10: use Nette;
11:
12:
13: /**
14: * DateTime.
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: return $tmp->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
55:
56: } else { // textual or NULL
57: return new static($time);
58: }
59: }
60:
61:
62: /**
63: * @return string
64: */
65: public function __toString()
66: {
67: return $this->format('Y-m-d H:i:s');
68: }
69:
70:
71: /**
72: * @param string
73: * @return self
74: */
75: public function modifyClone($modify = '')
76: {
77: $dolly = clone $this;
78: return $modify ? $dolly->modify($modify) : $dolly;
79: }
80:
81:
82: /**
83: * @param int
84: * @return self
85: */
86: public function setTimestamp($timestamp)
87: {
88: $zone = $this->getTimezone();
89: $this->__construct('@' . $timestamp);
90: return $this->setTimeZone($zone);
91: }
92:
93:
94: /**
95: * @return int|string
96: */
97: public function getTimestamp()
98: {
99: $ts = $this->format('U');
100: return is_float($tmp = $ts * 1) ? $ts : $tmp;
101: }
102:
103: }
104: