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: */
7:
8: namespace Nette;
9:
10: use Nette;
11:
12:
13: /**
14: * DateTime.
15: *
16: * @author David Grudl
17: */
18: class DateTime extends \DateTime
19: {
20: /** minute in seconds */
21: const MINUTE = 60;
22:
23: /** hour in seconds */
24: const HOUR = 3600;
25:
26: /** day in seconds */
27: const DAY = 86400;
28:
29: /** week in seconds */
30: const WEEK = 604800;
31:
32: /** average month in seconds */
33: const MONTH = 2629800;
34:
35: /** average year in seconds */
36: const YEAR = 31557600;
37:
38:
39: /**
40: * DateTime object factory.
41: * @param string|int|\DateTime
42: * @return DateTime
43: */
44: public static function from($time)
45: {
46: if ($time instanceof \DateTime || $time instanceof \DateTimeInterface) {
47: return new static($time->format('Y-m-d H:i:s'), $time->getTimezone());
48:
49: } elseif (is_numeric($time)) {
50: if ($time <= self::YEAR) {
51: $time += time();
52: }
53: $tmp = new static('@' . $time);
54: return $tmp->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
55:
56: } else { // textual or NULL
57: return new static($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 = $this->getTimezone();
78: $this->__construct('@' . $timestamp);
79: return $this->setTimeZone($zone);
80: }
81:
82:
83: public function getTimestamp()
84: {
85: $ts = $this->format('U');
86: return is_float($tmp = $ts * 1) ? $ts : $tmp;
87: }
88:
89: }
90: