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