Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7
 
8
declare(strict_types=1);
9
 
10
namespace Nette\Utils;
11
 
12
use Nette;
13
 
14
 
15
/**
16
 * DateTime.
17
 */
18
class DateTime extends \DateTime implements \JsonSerializable
19
{
20
	use Nette\SmartObject;
21
 
22
	/** minute in seconds */
23
	public const MINUTE = 60;
24
 
25
	/** hour in seconds */
26
	public const HOUR = 60 * self::MINUTE;
27
 
28
	/** day in seconds */
29
	public const DAY = 24 * self::HOUR;
30
 
31
	/** week in seconds */
32
	public const WEEK = 7 * self::DAY;
33
 
34
	/** average month in seconds */
35
	public const MONTH = 2629800;
36
 
37
	/** average year in seconds */
38
	public const YEAR = 31557600;
39
 
40
 
41
	/**
42
	 * Creates a DateTime object from a string, UNIX timestamp, or other DateTimeInterface object.
43
	 * @param  string|int|\DateTimeInterface  $time
44
	 * @return static
45
	 * @throws \Exception if the date and time are not valid.
46
	 */
47
	public static function from($time)
48
	{
49
		if ($time instanceof \DateTimeInterface) {
50
			return new static($time->format('Y-m-d H:i:s.u'), $time->getTimezone());
51
 
52
		} elseif (is_numeric($time)) {
53
			if ($time <= self::YEAR) {
54
				$time += time();
55
			}
56
 
57
			return (new static('@' . $time))->setTimezone(new \DateTimeZone(date_default_timezone_get()));
58
 
59
		} else { // textual or null
60
			return new static((string) $time);
61
		}
62
	}
63
 
64
 
65
	/**
66
	 * Creates DateTime object.
67
	 * @return static
68
	 * @throws Nette\InvalidArgumentException if the date and time are not valid.
69
	 */
70
	public static function fromParts(
71
		int $year,
72
		int $month,
73
		int $day,
74
		int $hour = 0,
75
		int $minute = 0,
76
		float $second = 0.0
77
	) {
78
		$s = sprintf('%04d-%02d-%02d %02d:%02d:%02.5F', $year, $month, $day, $hour, $minute, $second);
79
		if (
80
			!checkdate($month, $day, $year)
81
			|| $hour < 0
82
			|| $hour > 23
83
			|| $minute < 0
84
			|| $minute > 59
85
			|| $second < 0
86
			|| $second >= 60
87
		) {
88
			throw new Nette\InvalidArgumentException("Invalid date '$s'");
89
		}
90
 
91
		return new static($s);
92
	}
93
 
94
 
95
	/**
96
	 * Returns new DateTime object formatted according to the specified format.
97
	 * @param  string  $format  The format the $time parameter should be in
98
	 * @param  string  $time
99
	 * @param  string|\DateTimeZone  $timezone (default timezone is used if null is passed)
100
	 * @return static|false
101
	 */
102
	#[\ReturnTypeWillChange]
103
	public static function createFromFormat($format, $time, $timezone = null)
104
	{
105
		if ($timezone === null) {
106
			$timezone = new \DateTimeZone(date_default_timezone_get());
107
 
108
		} elseif (is_string($timezone)) {
109
			$timezone = new \DateTimeZone($timezone);
110
 
111
		} elseif (!$timezone instanceof \DateTimeZone) {
112
			throw new Nette\InvalidArgumentException('Invalid timezone given');
113
		}
114
 
115
		$date = parent::createFromFormat($format, $time, $timezone);
116
		return $date ? static::from($date) : false;
117
	}
118
 
119
 
120
	/**
121
	 * Returns JSON representation in ISO 8601 (used by JavaScript).
122
	 */
123
	public function jsonSerialize(): string
124
	{
125
		return $this->format('c');
126
	}
127
 
128
 
129
	/**
130
	 * Returns the date and time in the format 'Y-m-d H:i:s'.
131
	 */
132
	public function __toString(): string
133
	{
134
		return $this->format('Y-m-d H:i:s');
135
	}
136
 
137
 
138
	/**
139
	 * Creates a copy with a modified time.
140
	 * @return static
141
	 */
142
	public function modifyClone(string $modify = '')
143
	{
144
		$dolly = clone $this;
145
		return $modify ? $dolly->modify($modify) : $dolly;
146
	}
147
}