Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
date.php
1<?php
2
3namespace Bitrix\Main\Type;
4
7
8class Date
9{
11 protected $value;
12
19 public function __construct($date = null, $format = null)
20 {
21 $this->value = new \DateTime();
22 if ($date !== null && $date !== "")
23 {
24 if ($format === null)
25 {
26 $format = static::getFormat();
27 }
28
29 $parsedValue = $this->parse($format, $date);
30
31 if($parsedValue === false)
32 {
33 throw new Main\ObjectException("Incorrect date: ".$date);
34 }
35
36 if(isset($parsedValue["timestamp"]))
37 {
38 $this->value->setTimestamp($parsedValue["timestamp"]);
39 }
40 else
41 {
42 $this->value->setDate($parsedValue['year'], $parsedValue['month'], $parsedValue['day']);
43 }
44 }
45 $this->value->setTime(0, 0);
46 }
47
53 protected function parse($format, $time)
54 {
55 $parsedValue = date_parse_from_format($format, $time);
56
57 //Ignore errors when format is longer than date
58 //or date string is longer than format
59 if ($parsedValue['error_count'] > 1)
60 {
61 $error = current($parsedValue['errors']);
62
63 if ($error === 'A two digit second could not be found')
64 {
65 //possibly missed seconds with am/pm format
66 $timestamp = strtotime($time);
67
68 if ($timestamp === false)
69 {
70 return false;
71 }
72
73 return [
74 "timestamp" => $timestamp,
75 ];
76 }
77 if ($error !== 'Trailing data' && $error !== 'Data missing')
78 {
79 return false;
80 }
81 }
82
83 if(isset($parsedValue["relative"]["second"]) && $parsedValue["relative"]["second"] <> 0)
84 {
85 return [
86 "timestamp" => $parsedValue["relative"]["second"],
87 ];
88 }
89
90 //normalize values
91 if($parsedValue['month'] === false)
92 {
93 $parsedValue['month'] = 1;
94 }
95 if($parsedValue['day'] === false)
96 {
97 $parsedValue['day'] = 1;
98 }
99
100 return $parsedValue;
101 }
102
110 public function format($format)
111 {
112 return $this->value->format($format);
113 }
114
120 public function __clone()
121 {
122 $this->value = clone $this->value;
123 }
124
145 public function add($interval)
146 {
147 $i = $this->tryToCreateIntervalByDesignators($interval);
148 if ($i == null)
149 {
150 $i = \DateInterval::createFromDateString($interval);
151 }
152
153 $this->value->add($i);
154
155 return $this;
156 }
157
166 public function setDate($year, $month, $day)
167 {
168 $this->value->setDate($year, $month, $day);
169
170 return $this;
171 }
172
173 private function tryToCreateIntervalByDesignators($interval)
174 {
175 if (!is_string($interval) || strpos($interval, ' ') !== false)
176 {
177 return null;
178 }
179
180 $i = null;
181 try
182 {
183 $intervalTmp = strtoupper($interval);
184 $isNegative = false;
185 $firstChar = substr($intervalTmp, 0, 1);
186 if ($firstChar === "-")
187 {
188 $isNegative = true;
189 $intervalTmp = substr($intervalTmp, 1);
190 $firstChar = substr($intervalTmp, 0, 1);
191 }
192
193 if ($firstChar !== "P")
194 {
195 $intervalTmp = "P".$intervalTmp;
196 }
197 $i = new \DateInterval($intervalTmp);
198 if ($isNegative)
199 {
200 $i->invert = 1;
201 }
202 }
203 catch (\Exception $e)
204 {
205 }
206
207 return $i;
208 }
209
215 public function getTimestamp()
216 {
217 return $this->value->getTimestamp();
218 }
219
226 public function getDiff(Date $time)
227 {
228 return $this->value->diff($time->value);
229 }
230
238 public function toString(Context\Culture $culture = null)
239 {
240 $format = static::getFormat($culture);
241 return $this->format($format);
242 }
243
249 public function __toString()
250 {
251 return $this->toString();
252 }
253
261 public static function getFormat(Context\Culture $culture = null)
262 {
263 static $defaultCulture = null;
264
265 if($culture === null)
266 {
267 if($defaultCulture === null)
268 {
269 $context = Context::getCurrent();
270 if($context)
271 {
272 $defaultCulture = $context->getCulture();
273 }
274 }
275 $culture = $defaultCulture;
276 }
277
278 $format = static::getCultureFormat($culture);
279
280 return static::convertFormatToPhp($format);
281 }
282
290 protected static function getCultureFormat(Context\Culture $culture = null)
291 {
292 if($culture)
293 {
294 return $culture->getDateFormat();
295 }
296 return "DD.MM.YYYY";
297 }
298
306 public static function convertFormatToPhp($format)
307 {
308 static $from = array(
309 "YYYY", // 1999
310 "MMMM", // January - December
311 "MM", // 01 - 12
312 "DD", // 01 - 31
313 "TT", // AM - PM
314 "T", // am - pm
315 "MI", // 00 - 59
316 "SS", // 00 - 59
317 );
318 static $to = array(
319 "Y", // 1999
320 "F", // January - December
321 "m", // 01 - 12
322 "d", // 01 - 31
323 "A", // AM - PM
324 "a", // am - pm
325 "i", // 00 - 59
326 "s", // 00 - 59
327 );
328
329 $format = str_replace($from, $to, $format);
330
331 $tempFormat = $format;
332 $format = str_replace("HH", "H", $format); // 00 - 24
333 if ($tempFormat === $format)
334 {
335 $format = str_replace("H", "h", $format); // 01 - 12
336 }
337
338 $tempFormat = $format;
339 $format = str_replace("GG", "G", $format); // 0 - 24
340 if ($tempFormat === $format)
341 {
342 $format = str_replace("G", "g", $format); // 1 - 12
343 }
344
345 return $format;
346 }
347
356 public static function isCorrect($time, $format = null)
357 {
358 if (empty($time))
359 {
360 return false;
361 }
362
363 $result = true;
364
365 try
366 {
367 new static($time, $format);
368 }
369 catch (Main\ObjectException $ex)
370 {
371 $result = false;
372 }
373
374 return $result;
375 }
376
384 public static function createFromPhp(\DateTime $datetime)
385 {
386 $d = new static();
387 $d->value = clone $datetime;
388 $d->value->setTime(0, 0);
389 return $d;
390 }
391
399 public static function createFromTimestamp($timestamp)
400 {
401 $d = new static();
402 $d->value->setTimestamp($timestamp);
403 $d->value->setTime(0, 0);
404 return $d;
405 }
406
414 public static function createFromText($text)
415 {
416 $result = Main\Text\DateConverter::decode($text);
417 if (empty($result))
418 {
419 return null;
420 }
421
422 return $result[0]->getDate();
423 }
424}
static getCurrent()
Definition context.php:241
static createFromTimestamp($timestamp)
Definition date.php:399
static getFormat(Context\Culture $culture=null)
Definition date.php:261
add($interval)
Definition date.php:145
setDate($year, $month, $day)
Definition date.php:166
toString(Context\Culture $culture=null)
Definition date.php:238
static getCultureFormat(Context\Culture $culture=null)
Definition date.php:290
getDiff(Date $time)
Definition date.php:226
parse($format, $time)
Definition date.php:53
__construct($date=null, $format=null)
Definition date.php:19
static convertFormatToPhp($format)
Definition date.php:306
static createFromText($text)
Definition date.php:414
static isCorrect($time, $format=null)
Definition date.php:356
static createFromPhp(\DateTime $datetime)
Definition date.php:384
setTime($hour, $minute, $second=0, $microseconds=0)
Definition datetime.php:144