1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004, 2011 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: */
20: class NDateTime53 extends DateTime
21: {
22: /** minute in seconds */
23: const MINUTE = 60;
24:
25: /** hour in seconds */
26: const HOUR = 3600;
27:
28: /** day in seconds */
29: const DAY = 86400;
30:
31: /** week in seconds */
32: const WEEK = 604800;
33:
34: /** average month in seconds */
35: const MONTH = 2629800;
36:
37: /** average year in seconds */
38: const YEAR = 31557600;
39:
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 clone $time;
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 static function __set_state($state)
65: {
66: return new self($state['date'], new NDateTimeZone($state['timezone']));
67: }
68:
69:
70:
71: public function __sleep()
72: {
73: $this->fix = array($this->format('Y-m-d H:i:s'), $this->getTimezone()->getName());
74: return array('fix');
75: }
76:
77:
78:
79: public function __wakeup()
80: {
81: $this->__construct($this->fix[0], new NDateTimeZone($this->fix[1]));
82: unset($this->fix);
83: }
84:
85:
86:
87: public function getTimestamp()
88: {
89: return (int) $this->format('U');
90: }
91:
92:
93:
94: public function setTimestamp($timestamp)
95: {
96: return $this->__construct(
97: gmdate('Y-m-d H:i:s', $timestamp + $this->getOffset()),
98: new NDateTimeZone($this->getTimezone()->getName()) // simply getTimezone() crashes in PHP 5.2.6
99: );
100: }
101:
102: }
103: