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: * DateTime object factory.
44: * @param string|int|DateTime
45: * @return NDateTime53
46: */
47: public static function from($time)
48: {
49: if ($time instanceof DateTime) {
50: return new self($time->format('Y-m-d H:i:s'), $time->getTimezone());
51:
52: } elseif (is_numeric($time)) {
53: if ($time <= self::YEAR) {
54: $time += time();
55: }
56: return new self(date('Y-m-d H:i:s', $time));
57:
58: } else { // textual or NULL
59: return new self($time);
60: }
61: }
62:
63:
64: public function __toString()
65: {
66: return $this->format('Y-m-d H:i:s');
67: }
68:
69:
70: public function modifyClone($modify = '')
71: {
72: $dolly = clone $this;
73: return $modify ? $dolly->modify($modify) : $dolly;
74: }
75:
76:
77: public function modify($modify)
78: {
79: parent::modify($modify);
80: return $this;
81: }
82:
83:
84: public static function __set_state($state)
85: {
86: return new self($state['date'], new DateTimeZone($state['timezone']));
87: }
88:
89:
90: public function __sleep()
91: {
92: $this->fix = array($this->format('Y-m-d H:i:s'), $this->getTimezone()->getName());
93: return array('fix');
94: }
95:
96:
97: public function __wakeup()
98: {
99: $this->__construct($this->fix[0], new DateTimeZone($this->fix[1]));
100: unset($this->fix);
101: }
102:
103:
104: public function getTimestamp()
105: {
106: return (int) $this->format('U');
107: }
108:
109:
110: public function setTimestamp($timestamp)
111: {
112: return $this->__construct(
113: gmdate('Y-m-d H:i:s', $timestamp + $this->getOffset()),
114: new DateTimeZone($this->getTimezone()->getName()) // simply getTimezone() crashes in PHP 5.2.6
115: );
116: }
117:
118: }
119: