1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
date.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Type;
4
5use Bitrix\Main;
6use Bitrix\Main\Context;
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 if ($i instanceof \DateInterval)
154 {
155 $this->value->add($i);
156 }
157
158 return $this;
159 }
160
169 public function setDate($year, $month, $day)
170 {
171 $this->value->setDate($year, $month, $day);
172
173 return $this;
174 }
175
176 private function tryToCreateIntervalByDesignators($interval)
177 {
178 if (!is_string($interval) || str_contains($interval, ' '))
179 {
180 return null;
181 }
182
183 $i = null;
184 try
185 {
186 $intervalTmp = strtoupper($interval);
187 $isNegative = false;
188 $firstChar = substr($intervalTmp, 0, 1);
189 if ($firstChar === "-")
190 {
191 $isNegative = true;
192 $intervalTmp = substr($intervalTmp, 1);
193 $firstChar = substr($intervalTmp, 0, 1);
194 }
195
196 if ($firstChar !== "P")
197 {
198 $intervalTmp = "P" . $intervalTmp;
199 }
200 $i = new \DateInterval($intervalTmp);
201 if ($isNegative)
202 {
203 $i->invert = 1;
204 }
205 }
206 catch (\Exception)
207 {
208 }
209
210 return $i;
211 }
212
218 public function getTimestamp()
219 {
220 return $this->value->getTimestamp();
221 }
222
229 public function getDiff(Date $time)
230 {
231 return $this->value->diff($time->value);
232 }
233
241 public function toString(Context\Culture $culture = null)
242 {
243 $format = static::getFormat($culture);
244 return $this->format($format);
245 }
246
252 public function __toString()
253 {
254 return $this->toString();
255 }
256
264 public static function getFormat(Context\Culture $culture = null)
265 {
266 static $defaultCulture = null;
267
268 if ($culture === null)
269 {
270 if ($defaultCulture === null)
271 {
272 $context = Context::getCurrent();
273 if ($context)
274 {
275 $defaultCulture = $context->getCulture();
276 }
277 }
278 $culture = $defaultCulture;
279 }
280
281 $format = static::getCultureFormat($culture);
282
283 return static::convertFormatToPhp($format);
284 }
285
293 protected static function getCultureFormat(Context\Culture $culture = null)
294 {
295 if ($culture)
296 {
297 return $culture->getDateFormat();
298 }
299 return "DD.MM.YYYY";
300 }
301
309 public static function convertFormatToPhp($format)
310 {
311 static $from = [
312 "YYYY", // 1999
313 "MMMM", // January - December
314 "MM", // 01 - 12
315 "DD", // 01 - 31
316 "TT", // AM - PM
317 "T", // am - pm
318 "MI", // 00 - 59
319 "SS", // 00 - 59
320 ];
321 static $to = [
322 "Y", // 1999
323 "F", // January - December
324 "m", // 01 - 12
325 "d", // 01 - 31
326 "A", // AM - PM
327 "a", // am - pm
328 "i", // 00 - 59
329 "s", // 00 - 59
330 ];
331
332 $format = str_replace($from, $to, $format);
333
334 $tempFormat = $format;
335 $format = str_replace("HH", "H", $format); // 00 - 24
336 if ($tempFormat === $format)
337 {
338 $format = str_replace("H", "h", $format); // 01 - 12
339 }
340
341 $tempFormat = $format;
342 $format = str_replace("GG", "G", $format); // 0 - 24
343 if ($tempFormat === $format)
344 {
345 $format = str_replace("G", "g", $format); // 1 - 12
346 }
347
348 return $format;
349 }
350
359 public static function isCorrect($time, $format = null)
360 {
361 if (empty($time))
362 {
363 return false;
364 }
365
366 $result = true;
367
368 try
369 {
370 new static($time, $format);
371 }
372 catch (Main\ObjectException)
373 {
374 $result = false;
375 }
376
377 return $result;
378 }
379
387 public static function createFromPhp(\DateTime $datetime)
388 {
389 $d = new static();
390 $d->value = clone $datetime;
391 $d->value->setTime(0, 0);
392 return $d;
393 }
394
402 public static function createFromTimestamp($timestamp)
403 {
404 $d = new static();
405 $d->value->setTimestamp($timestamp);
406 $d->value->setTime(0, 0);
407 return $d;
408 }
409
417 public static function createFromText($text)
418 {
420 if (empty($result))
421 {
422 return null;
423 }
424
425 return $result[0]->getDate();
426 }
427}
static decode($text, $limit=0)
Определения dateconverter.php:17
Определения date.php:9
static createFromTimestamp($timestamp)
Определения date.php:402
$value
Определения date.php:11
static getFormat(Context\Culture $culture=null)
Определения date.php:264
add($interval)
Определения date.php:145
setDate($year, $month, $day)
Определения date.php:169
format($format)
Определения date.php:110
__toString()
Определения date.php:252
toString(Context\Culture $culture=null)
Определения date.php:241
static getCultureFormat(Context\Culture $culture=null)
Определения date.php:293
getDiff(Date $time)
Определения date.php:229
getTimestamp()
Определения date.php:218
parse($format, $time)
Определения date.php:53
__construct($date=null, $format=null)
Определения date.php:19
__clone()
Определения date.php:120
static convertFormatToPhp($format)
Определения date.php:309
static createFromText($text)
Определения date.php:417
static isCorrect($time, $format=null)
Определения date.php:359
static createFromPhp(\DateTime $datetime)
Определения date.php:387
setTime($hour, $minute, $second=0, $microseconds=0)
Определения datetime.php:144
$result
Определения get_property_values.php:14
$context
Определения csv_new_setup.php:223
$culture
Определения include.php:61
Определения culture.php:9
$year
Определения payment.php:9
$time
Определения payment.php:61
$text
Определения template_pdf.php:79
$i
Определения factura.php:643
$error
Определения subscription_card_product.php:20