Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Timezone.php
Go to the documentation of this file.
1 <?php
8 
15 
21 class Timezone implements TimezoneInterface
22 {
26  protected $_allowedFormats = [
27  \IntlDateFormatter::FULL,
28  \IntlDateFormatter::LONG,
29  \IntlDateFormatter::MEDIUM,
30  \IntlDateFormatter::SHORT,
31  ];
32 
36  protected $_scopeType;
37 
41  protected $_scopeResolver;
42 
46  protected $_dateTime;
47 
52 
56  protected $_scopeConfig;
57 
61  protected $_localeResolver;
62 
71  public function __construct(
72  ScopeResolverInterface $scopeResolver,
73  ResolverInterface $localeResolver,
74  \Magento\Framework\Stdlib\DateTime $dateTime,
75  ScopeConfigInterface $scopeConfig,
76  $scopeType,
77  $defaultTimezonePath
78  ) {
79  $this->_scopeResolver = $scopeResolver;
80  $this->_localeResolver = $localeResolver;
81  $this->_dateTime = $dateTime;
82  $this->_defaultTimezonePath = $defaultTimezonePath;
83  $this->_scopeConfig = $scopeConfig;
84  $this->_scopeType = $scopeType;
85  }
86 
90  public function getDefaultTimezonePath()
91  {
93  }
94 
98  public function getDefaultTimezone()
99  {
100  return 'UTC';
101  }
102 
106  public function getConfigTimezone($scopeType = null, $scopeCode = null)
107  {
108  return $this->_scopeConfig->getValue(
109  $this->getDefaultTimezonePath(),
110  $scopeType ?: $this->_scopeType,
111  $scopeCode
112  );
113  }
114 
118  public function getDateFormat($type = \IntlDateFormatter::SHORT)
119  {
120  return (new \IntlDateFormatter(
121  $this->_localeResolver->getLocale(),
122  $type,
123  \IntlDateFormatter::NONE
124  ))->getPattern();
125  }
126 
130  public function getDateFormatWithLongYear()
131  {
132  return preg_replace(
133  '/(?<!y)yy(?!y)/',
134  'Y',
135  $this->getDateFormat()
136  );
137  }
138 
142  public function getTimeFormat($type = \IntlDateFormatter::SHORT)
143  {
144  return (new \IntlDateFormatter(
145  $this->_localeResolver->getLocale(),
146  \IntlDateFormatter::NONE,
147  $type
148  ))->getPattern();
149  }
150 
154  public function getDateTimeFormat($type)
155  {
156  return $this->getDateFormat($type) . ' ' . $this->getTimeFormat($type);
157  }
158 
162  public function date($date = null, $locale = null, $useTimezone = true, $includeTime = true)
163  {
164  $locale = $locale ?: $this->_localeResolver->getLocale();
165  $timezone = $useTimezone
166  ? $this->getConfigTimezone()
167  : date_default_timezone_get();
168 
169  switch (true) {
170  case (empty($date)):
171  return new \DateTime('now', new \DateTimeZone($timezone));
172  case ($date instanceof \DateTime):
173  return $date->setTimezone(new \DateTimeZone($timezone));
174  case ($date instanceof \DateTimeImmutable):
175  return new \DateTime($date->format('Y-m-d H:i:s'), $date->getTimezone());
176  case (!is_numeric($date)):
177  $timeType = $includeTime ? \IntlDateFormatter::SHORT : \IntlDateFormatter::NONE;
178  $formatter = new \IntlDateFormatter(
179  $locale,
180  \IntlDateFormatter::SHORT,
181  $timeType,
182  new \DateTimeZone($timezone)
183  );
184 
185  $date = $this->appendTimeIfNeeded($date, $includeTime);
186  $date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp();
187  break;
188  }
189 
190  return (new \DateTime(null, new \DateTimeZone($timezone)))->setTimestamp($date);
191  }
192 
196  public function scopeDate($scope = null, $date = null, $includeTime = false)
197  {
198  $timezone = $this->_scopeConfig->getValue($this->getDefaultTimezonePath(), $this->_scopeType, $scope);
199  $date = new \DateTime(is_numeric($date) ? '@' . $date : $date, new \DateTimeZone($timezone));
200  if (!$includeTime) {
201  $date->setTime(0, 0, 0);
202  }
203  return $date;
204  }
205 
209  public function formatDate($date = null, $format = \IntlDateFormatter::SHORT, $showTime = false)
210  {
211  $formatTime = $showTime ? $format : \IntlDateFormatter::NONE;
212 
213  if (!($date instanceof \DateTimeInterface)) {
214  $date = new \DateTime($date);
215  }
216 
217  return $this->formatDateTime($date, $format, $formatTime);
218  }
219 
223  public function scopeTimeStamp($scope = null)
224  {
225  $timezone = $this->_scopeConfig->getValue($this->getDefaultTimezonePath(), $this->_scopeType, $scope);
226  $currentTimezone = @date_default_timezone_get();
227  @date_default_timezone_set($timezone);
228  $date = date('Y-m-d H:i:s');
229  @date_default_timezone_set($currentTimezone);
230  return strtotime($date);
231  }
232 
236  public function isScopeDateInInterval($scope, $dateFrom = null, $dateTo = null)
237  {
238  if (!$scope instanceof ScopeInterface) {
239  $scope = $this->_scopeResolver->getScope($scope);
240  }
241 
242  $scopeTimeStamp = $this->scopeTimeStamp($scope);
243  $fromTimeStamp = strtotime($dateFrom);
244  $toTimeStamp = strtotime($dateTo);
245  if ($dateTo) {
246  // fix date YYYY-MM-DD 00:00:00 to YYYY-MM-DD 23:59:59
247  $toTimeStamp += 86400;
248  }
249 
250  $result = false;
251  if (!$this->_dateTime->isEmptyDate($dateFrom) && $scopeTimeStamp < $fromTimeStamp) {
252  } elseif (!$this->_dateTime->isEmptyDate($dateTo) && $scopeTimeStamp > $toTimeStamp) {
253  } else {
254  $result = true;
255  }
256  return $result;
257  }
258 
268  public function formatDateTime(
269  $date,
270  $dateType = \IntlDateFormatter::SHORT,
271  $timeType = \IntlDateFormatter::SHORT,
272  $locale = null,
273  $timezone = null,
274  $pattern = null
275  ) {
276  if (!($date instanceof \DateTimeInterface)) {
277  $date = new \DateTime($date);
278  }
279 
280  if ($timezone === null) {
281  if ($date->getTimezone() == null || $date->getTimezone()->getName() == 'UTC'
282  || $date->getTimezone()->getName() == '+00:00'
283  ) {
284  $timezone = $this->getConfigTimezone();
285  } else {
286  $timezone = $date->getTimezone();
287  }
288  }
289 
290  $formatter = new \IntlDateFormatter(
291  $locale ?: $this->_localeResolver->getLocale(),
292  $dateType,
293  $timeType,
294  $timezone,
295  null,
296  $pattern
297  );
298  return $formatter->format($date);
299  }
300 
310  public function convertConfigTimeToUtc($date, $format = 'Y-m-d H:i:s')
311  {
312  if (!($date instanceof \DateTimeInterface)) {
313  if ($date instanceof \DateTimeImmutable) {
314  $date = new \DateTime($date->format('Y-m-d H:i:s'), new \DateTimeZone($this->getConfigTimezone()));
315  } else {
316  $date = new \DateTime($date, new \DateTimeZone($this->getConfigTimezone()));
317  }
318  } else {
319  if ($date->getTimezone()->getName() !== $this->getConfigTimezone()) {
320  throw new LocalizedException(
321  new Phrase(
322  'The DateTime object timezone needs to be the same as the "%1" timezone in config.',
323  $this->getConfigTimezone()
324  )
325  );
326  }
327  }
328 
329  $date->setTimezone(new \DateTimeZone('UTC'));
330 
331  return $date->format($format);
332  }
333 
341  private function appendTimeIfNeeded($date, $includeTime)
342  {
343  if ($includeTime && !preg_match('/\d{1}:\d{2}/', $date)) {
344  $date .= " 0:00am";
345  }
346  return $date;
347  }
348 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
date($date=null, $locale=null, $useTimezone=true, $includeTime=true)
Definition: Timezone.php:162
$pattern
Definition: website.php:22
formatDate($date=null, $format=\IntlDateFormatter::SHORT, $showTime=false)
Definition: Timezone.php:209
formatDateTime( $date, $dateType=\IntlDateFormatter::SHORT, $timeType=\IntlDateFormatter::SHORT, $locale=null, $timezone=null, $pattern=null)
Definition: Timezone.php:268
$type
Definition: item.phtml:13
$format
Definition: list.phtml:12
getDateFormat($type=\IntlDateFormatter::SHORT)
Definition: Timezone.php:118
getConfigTimezone($scopeType=null, $scopeCode=null)
Definition: Timezone.php:106
scopeDate($scope=null, $date=null, $includeTime=false)
Definition: Timezone.php:196
convertConfigTimeToUtc($date, $format='Y-m-d H:i:s')
Definition: Timezone.php:310
getTimeFormat($type=\IntlDateFormatter::SHORT)
Definition: Timezone.php:142
__construct(ScopeResolverInterface $scopeResolver, ResolverInterface $localeResolver, \Magento\Framework\Stdlib\DateTime $dateTime, ScopeConfigInterface $scopeConfig, $scopeType, $defaultTimezonePath)
Definition: Timezone.php:71
isScopeDateInInterval($scope, $dateFrom=null, $dateTo=null)
Definition: Timezone.php:236
$dateTime