Bitrix-D7  20.5.0
main/lib/type/datetime.php
См. документацию.
1 <?php
2 namespace Bitrix\Main\Type;
3 
4 use Bitrix\Main;
6 
7 class DateTime extends Date
8 {
9  /**
10  * @param string $time String representation of datetime.
11  * @param string $format PHP datetime format. If not specified, the format is got from the current culture.
12  * @param \DateTimeZone $timezone Optional timezone object.
13  *
14  * @throws Main\ObjectException
15  */
16  public function __construct($time = null, $format = null, \DateTimeZone $timezone = null)
17  {
18  if ($timezone === null)
19  {
20  $this->value = new \DateTime();
21  }
22  else
23  {
24  $this->value = new \DateTime(null, $timezone);
25  }
26 
27  if ($time !== null && $time !== "")
28  {
29  if ($format === null)
30  {
31  $format = static::getFormat();
32  }
33 
34  $parsedValue = $this->parse($format, $time);
35 
36  if($parsedValue === false)
37  {
38  throw new Main\ObjectException("Incorrect date/time: ".$time);
39  }
40 
41  if(isset($parsedValue["timestamp"]))
42  {
43  $this->value->setTimestamp($parsedValue["timestamp"]);
44  }
45  else
46  {
47  if(isset($parsedValue["zone_type"]) && $parsedValue["zone_type"] == 1)
48  {
49  if(isset($parsedValue["zone"]) && $parsedValue["zone"] <> 0)
50  {
51  $this->setTimeZone(new \DateTimeZone(static::secondsToOffset($parsedValue["zone"])));
52  }
53  }
54 
55  $microseconds = 0;
56  if($parsedValue['fraction'] > 0)
57  {
58  $microseconds = intval($parsedValue['fraction'] * 1000000);
59  }
60 
61  $this->value->setDate($parsedValue['year'], $parsedValue['month'], $parsedValue['day']);
62  $this->value->setTime($parsedValue['hour'], $parsedValue['minute'], $parsedValue['second'], $microseconds);
63  }
64  }
65  }
66 
67  public static function secondsToOffset($seconds)
68  {
69  $absSeconds = abs($seconds);
70  $hours = sprintf("%02d", floor($absSeconds / 3600));
71  $minutes = gmdate("i", $absSeconds % 3600);
72  return ($seconds < 0? "-" : "+").$hours.$minutes;
73  }
74 
75  /**
76  * Converts date to string, using Culture and global timezone settings.
77  *
78  * @param Context\Culture $culture Culture contains datetime format.
79  *
80  * @return string
81  */
82  public function toString(Context\Culture $culture = null)
83  {
84  if(\CTimeZone::Enabled())
85  {
86  $userTime = clone $this;
87  $userTime->toUserTime();
88 
89  $format = static::getFormat($culture);
90  return $userTime->format($format);
91  }
92  else
93  {
94  return parent::toString($culture);
95  }
96  }
97 
98  /**
99  * Returns timezone object.
100  *
101  * @return \DateTimeZone
102  */
103  public function getTimeZone()
104  {
105  return $this->value->getTimezone();
106  }
107 
108  /**
109  * Sets timezone object.
110  *
111  * @param \DateTimeZone $timezone Timezone object.
112  *
113  * @return DateTime
114  */
115  public function setTimeZone(\DateTimeZone $timezone)
116  {
117  $this->value->setTimezone($timezone);
118  return $this;
119  }
120 
121  /**
122  * Sets default timezone.
123  *
124  * @return DateTime
125  */
126  public function setDefaultTimeZone()
127  {
128  $time = new \DateTime();
129  $this->setTimezone($time->getTimezone());
130  return $this;
131  }
132 
133  /**
134  * @param int $hour Hour value.
135  * @param int $minute Minute value.
136  * @param int $second Second value.
137  * @param int $microseconds Microseconds value.
138  *
139  * @return DateTime
140  */
141  public function setTime($hour, $minute, $second = 0, $microseconds = 0)
142  {
143  $this->value->setTime($hour, $minute, $second, $microseconds);
144  return $this;
145  }
146 
147  /**
148  * Changes time from server time to user time using global timezone settings.
149  *
150  * @return DateTime
151  */
152  public function toUserTime()
153  {
154  //first, move to server timezone
155  $this->setDefaultTimeZone();
156 
157  //second, adjust time according global timezone offset
158  static $diff = null;
159  if($diff === null)
160  {
161  $diff = \CTimeZone::GetOffset();
162  }
163  if($diff <> 0)
164  {
165  $this->add(($diff < 0? "-":"")."PT".abs($diff)."S");
166  }
167  return $this;
168  }
169 
170  /**
171  * Creates DateTime object from local user time using global timezone settings and default culture.
172  *
173  * @param string $timeString Full or short formatted time.
174  *
175  * @return DateTime
176  */
177  public static function createFromUserTime($timeString)
178  {
179  /** @var DateTime $time */
180  try
181  {
182  //try full datetime format
183  $time = new static($timeString);
184  }
185  catch(Main\ObjectException $e)
186  {
187  //try short date format
188  $time = new static($timeString, Date::getFormat());
189  $time->setTime(0, 0, 0);
190  }
191 
192  if(\CTimeZone::Enabled())
193  {
194  static $diff = null;
195  if($diff === null)
196  {
197  $diff = \CTimeZone::GetOffset();
198  }
199  if($diff <> 0)
200  {
201  $time->add(($diff > 0? "-":"")."PT".abs($diff)."S");
202  }
203  }
204  return $time;
205  }
206 
207  /**
208  * Returns long (including time) date culture format.
209  *
210  * @param Context\Culture $culture Culture.
211  *
212  * @return string
213  */
214  protected static function getCultureFormat(Context\Culture $culture = null)
215  {
216  if($culture)
217  {
218  return $culture->getDateTimeFormat();
219  }
220  return "DD.MM.YYYY HH:MI:SS";
221  }
222 
223  /**
224  * Creates DateTime object from PHP \DateTime object.
225  *
226  * @param \DateTime $datetime Source object.
227  *
228  * @return static
229  */
230  public static function createFromPhp(\DateTime $datetime)
231  {
232  $d = new static();
233  $d->value = clone $datetime;
234  return $d;
235  }
236 
237  /**
238  * Creates DateTime object from Unix timestamp.
239  *
240  * @param int $timestamp Source timestamp.
241  *
242  * @return static
243  */
244  public static function createFromTimestamp($timestamp)
245  {
246  $d = new static();
247  $d->value->setTimestamp($timestamp);
248  return $d;
249  }
250 
251  /**
252  * Creates DateTime object from string.
253  * NULL will be returned on failure.
254  * @param string $timeString Full formatted time.
255  * @param string $format PHP datetime format. If not specified, the format is got from the current culture.
256  * @return DateTime|null
257  */
258  public static function tryParse($timeString, $format = null)
259  {
260  if($timeString === '')
261  {
262  return null;
263  }
264 
265  if ($format === null)
266  {
267  $format = static::getFormat();
268  }
269 
270  try
271  {
272  $time = new DateTime($timeString, $format);
273  }
274  catch(Main\ObjectException $e)
275  {
276  $time = null;
277  }
278  return $time;
279  }
280 }
Context of current request.
Exception is thrown when the object can't be constructed.
static getFormat(Context\Culture $culture=null)
Returns a date format from the culture in the php format.
add($interval)
Performs dates arithmetic.
static createFromTimestamp($timestamp)
Creates DateTime object from Unix timestamp.
getTimeZone()
Returns timezone object.
toUserTime()
Changes time from server time to user time using global timezone settings.
static tryParse($timeString, $format=null)
Creates DateTime object from string.
toString(Context\Culture $culture=null)
Converts date to string, using Culture and global timezone settings.
static getCultureFormat(Context\Culture $culture=null)
Returns long (including time) date culture format.
__construct($time=null, $format=null, \DateTimeZone $timezone=null)
setDefaultTimeZone()
Sets default timezone.
setTime($hour, $minute, $second=0, $microseconds=0)
static createFromUserTime($timeString)
Creates DateTime object from local user time using global timezone settings and default culture.
setTimeZone(\DateTimeZone $timezone)
Sets timezone object.
static createFromPhp(\DateTime $datetime)
Creates DateTime object from PHP \DateTime object.