1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://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 clone $time;
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: public static function __set_state($state)
66: {
67: return new self($state['date'], new DateTimeZone($state['timezone']));
68: }
69:
70:
71:
72: public function __sleep()
73: {
74: $this->fix = array($this->format('Y-m-d H:i:s'), $this->getTimezone()->getName());
75: return array('fix');
76: }
77:
78:
79:
80: public function __wakeup()
81: {
82: $this->__construct($this->fix[0], new DateTimeZone($this->fix[1]));
83: unset($this->fix);
84: }
85:
86:
87:
88: public function getTimestamp()
89: {
90: return (int) $this->format('U');
91: }
92:
93:
94:
95: public function setTimestamp($timestamp)
96: {
97: return $this->__construct(
98: gmdate('Y-m-d H:i:s', $timestamp + $this->getOffset()),
99: new DateTimeZone($this->getTimezone()->getName()) // simply getTimezone() crashes in PHP 5.2.6
100: );
101: }
102:
103: }
104: