Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
datetime.php
1<?php
2
11
13
15{
16 public static function getDateValue(?\Bitrix\Main\Type\DateTime $value = null): string
17 {
18 if ($value === null)
19 {
20 return '';
21 }
22
23 $timestamp = static::getDateTimestamp($value);
24 $format = static::getHumanDateTimeFormat($timestamp);
25
26 return static::formatDateTime($timestamp, $format);
27 }
28
35 protected static function formatDateTime($stamp, ?string $format = null): string
36 {
37 $simple = false;
38
39 // accept also FORMAT_DATE and FORMAT_DATETIME as ones of the legal formats
40 if (
41 (defined('FORMAT_DATE') && $format === FORMAT_DATE)
42 || (defined('FORMAT_DATETIME') && $format === FORMAT_DATETIME))
43 {
44 $format = \CDatabase::dateFormatToPHP($format);
45 $simple = true;
46 }
47
48 $default = static::getDateTimeFormat();
49 if ($format === false)
50 {
51 $format = $default;
52 $simple = true;
53 }
54
55 if ($simple)
56 {
57 // its a simple format, we can use a simpler function
58 return date($format, $stamp);
59 }
60
61 return FormatDate($format, $stamp);
62 }
63
64 protected static function getDateTimeFormat()
65 {
66 $format = (defined('FORMAT_DATETIME') ? FORMAT_DATETIME : \CSite::getDateFormat());
67
68 return \CDatabase::dateFormatToPHP($format);
69 }
70
75 protected static function getDateTimestamp(\Bitrix\Main\Type\DateTime $date): int
76 {
77 $timestamp = MakeTimeStamp($date);
78
79 if ($timestamp === false)
80 {
81 $timestamp = strtotime($date);
82 if ($timestamp !== false)
83 {
84 $timestamp += \CTimeZone::getOffset() - \Bitrix\Main\Type\DateTime::createFromTimestamp($timestamp)->getSecondGmt();
85 }
86 }
87
88 return $timestamp;
89 }
90
91 protected static function getHumanDateTimeFormat(int $timestamp): string
92 {
93 $dateFormat = static::getHumanDateFormat($timestamp);
94 $timeFormat = static::getHumanTimeFormat($timestamp);
95
96 return $dateFormat . ($timeFormat ? ", {$timeFormat}" : '');
97 }
98
99 protected static function getHumanDateFormat(int $timestamp): string
100 {
101 $culture = Context::getCurrent()->getCulture();
102
103 if (date('Y') !== date('Y', $timestamp))
104 {
105 return $culture->getLongDateFormat();
106 }
107
108 return $culture->getDayMonthFormat();
109 }
110
111 protected static function getHumanTimeFormat(int $timestamp): string
112 {
113 $timeFormat = '';
114 $culture = Context::getCurrent()->getCulture();
115
116 if (date('Hi', $timestamp) > 0)
117 {
118 $timeFormat = $culture->getShortTimeFormat();
119 }
120
121 return $timeFormat;
122 }
123
124}
static getCurrent()
Definition context.php:241
static getHumanDateTimeFormat(int $timestamp)
Definition datetime.php:91
static getHumanDateFormat(int $timestamp)
Definition datetime.php:99
static getDateTimestamp(\Bitrix\Main\Type\DateTime $date)
Definition datetime.php:75
static getHumanTimeFormat(int $timestamp)
Definition datetime.php:111
static getDateValue(?\Bitrix\Main\Type\DateTime $value=null)
Definition datetime.php:16
static formatDateTime($stamp, ?string $format=null)
Definition datetime.php:35