Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
date.php
1<?php
2
4
7use DateTimeZone;
8
9class Date extends BaseProperty
10{
14 private Type\Date $date;
18 private $format;
19
26 public static function createDateTimeFromFormat(string $date, string $format): ?Date
27 {
28 $timeZone = null;
29 $entity = \DateTime::createFromFormat($format, $date);
30
31 if ($entity && $entity->getTimeZone()->getName() === 'Z')
32 {
33 $timeZone = new DateTimeZone(Util::DEFAULT_TIMEZONE);
34 }
35
36 if ($entity)
37 {
38 return new self(new Type\DateTime($entity->format($format), $format, $timeZone), $format);
39 }
40
41 return null;
42 }
43
50 public static function createDateFromFormat(string $date, string $format): Date
51 {
52 return new self(new Type\Date($date, $format), $format);
53 }
54
60 public function __construct(Type\Date $date = null, string $dateFormat = null)
61 {
62 $this->date = $date ?? Util::getDateObject(null, false, (new \DateTime())->getTimezone()->getName());
63 $this->format = $dateFormat
64 ?? $this->date instanceof Type\DateTime
65 ? Type\Date::convertFormatToPhp(FORMAT_DATETIME)
66 : Type\Date::convertFormatToPhp(FORMAT_DATE)
67 ;
68 }
69
73 public function getFields(): array
74 {
75 return [
76 'date' => $this->date->format($this->format),
77 'timezone' => ($this->date instanceof Type\DateTime) ? $this->date->getTimeZone()->getName() : null,
78 ];
79 }
80
84 public function toString(): string
85 {
86 return $this->date->format($this->format);
87 }
88
93 public function setTimezone(DateTimeZone $timeZone): Date
94 {
95 if ($this->date instanceof Type\DateTime)
96 {
97 $this->date->setTimeZone($timeZone);
98 }
99
100 return $this;
101 }
102
107 public function format(string $format = null): string
108 {
109 return $this->date->format($format ?? $this->format);
110 }
111
118 public function add(string $addTime): Date
119 {
120 $object = clone $this;
121 $object->date->add($addTime);
122
123 return $object;
124 }
125
129 public function getTimestamp(): int
130 {
131 return $this->date->getTimestamp();
132 }
133
140 public function sub(string $subtractTime): Date
141 {
142 $object = clone $this;
143 $object->date->add("-{$subtractTime}");
144
145 return $object;
146 }
147
151 public function __clone()
152 {
153 $this->date = clone $this->date;
154 }
155
162 public function setTime(int $hour, int $minutes, int $seconds): Date
163 {
164 if ($this->date instanceof Type\DateTime)
165 {
166 $this->date->setTime($hour, $minutes, $seconds);
167 }
168
169 return $this;
170 }
171
175 public function resetTime(): Date
176 {
177 return $this->setTime(0,0,0);
178 }
179
183 public function isDateTime(): bool
184 {
185 return $this->date instanceof Type\DateTime;
186 }
187
191 public function getDate(): Type\Date
192 {
193 return $this->date;
194 }
195
199 public function getHour(): int
200 {
201 return (int)$this->date->format('H');
202 }
203
207 public function getMinutes(): int
208 {
209 return (int)$this->date->format('i');
210 }
211
215 public function getSeconds(): int
216 {
217 return (int)$this->date->format('s');
218 }
219
223 public function getMonth(): int
224 {
225 return (int)$this->date->format('m');
226 }
227
231 public function getDay(): int
232 {
233 return (int)$this->date->format('d');
234 }
235
239 public function getYear(): int
240 {
241 return (int)$this->date->format('Y');
242 }
243
247 public function isStartDay(): bool
248 {
249 return $this->getHour() === 0 && $this->getMinutes() === 0 && $this->getSeconds() === 0;
250 }
251
256 public function setDateTimeFormat(string $format): Date
257 {
258 $this->format = $format;
259
260 return $this;
261 }
262
266 public function getDateTimeFormat(): string
267 {
268 return $this->format;
269 }
270}
format(string $format=null)
Definition date.php:107
setTime(int $hour, int $minutes, int $seconds)
Definition date.php:162
static createDateTimeFromFormat(string $date, string $format)
Definition date.php:26
setTimezone(DateTimeZone $timeZone)
Definition date.php:93
__construct(Type\Date $date=null, string $dateFormat=null)
Definition date.php:60
setDateTimeFormat(string $format)
Definition date.php:256
static createDateFromFormat(string $date, string $format)
Definition date.php:50
sub(string $subtractTime)
Definition date.php:140
add(string $addTime)
Definition date.php:118
const DEFAULT_TIMEZONE
Definition util.php:22
static getDateObject(string $date=null, ?bool $fullDay=true, ?string $tz='UTC')
Definition util.php:102