1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: * @package Nette
11: */
12:
13:
14:
15: /**
16: * DateTime with serialization and timestamp support for PHP 5.2.
17: *
18: * @author David Grudl
19: * @package Nette
20: */
21: class NDateTime53 extends DateTime
22: {
23: /** minute in seconds */
24: const MINUTE = 60;
25:
26: /** hour in seconds */
27: const HOUR = 3600;
28:
29: /** day in seconds */
30: const DAY = 86400;
31:
32: /** week in seconds */
33: const WEEK = 604800;
34:
35: /** average month in seconds */
36: const MONTH = 2629800;
37:
38: /** average year in seconds */
39: const YEAR = 31557600;
40:
41:
42:
43: /**
44: * DateTime object factory.
45: * @param string|int|DateTime
46: * @return NDateTime53
47: */
48: public static function from($time)
49: {
50: if ($time instanceof DateTime) {
51: return new self($time->format('Y-m-d H:i:s'), $time->getTimezone());
52:
53: } elseif (is_numeric($time)) {
54: if ($time <= self::YEAR) {
55: $time += time();
56: }
57: return new self(date('Y-m-d H:i:s', $time));
58:
59: } else { // textual or NULL
60: return new self($time);
61: }
62: }
63:
64:
65:
66: public function __toString()
67: {
68: return $this->format('Y-m-d H:i:s');
69: }
70:
71:
72:
73: public function modifyClone($modify = '')
74: {
75: $dolly = clone $this;
76: return $modify ? $dolly->modify($modify) : $dolly;
77: }
78:
79:
80:
81: public function modify($modify)
82: {
83: parent::modify($modify);
84: return $this;
85: }
86:
87:
88:
89: public static function __set_state($state)
90: {
91: return new self($state['date'], new DateTimeZone($state['timezone']));
92: }
93:
94:
95:
96: public function __sleep()
97: {
98: $this->fix = array($this->format('Y-m-d H:i:s'), $this->getTimezone()->getName());
99: return array('fix');
100: }
101:
102:
103:
104: public function __wakeup()
105: {
106: $this->__construct($this->fix[0], new DateTimeZone($this->fix[1]));
107: unset($this->fix);
108: }
109:
110:
111:
112: public function getTimestamp()
113: {
114: return (int) $this->format('U');
115: }
116:
117:
118:
119: public function setTimestamp($timestamp)
120: {
121: return $this->__construct(
122: gmdate('Y-m-d H:i:s', $timestamp + $this->getOffset()),
123: new DateTimeZone($this->getTimezone()->getName()) // simply getTimezone() crashes in PHP 5.2.6
124: );
125: }
126:
127: }
128: