Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Timezone Class Reference
Inheritance diagram for Timezone:
TimezoneInterface

Public Member Functions

 __construct (ScopeResolverInterface $scopeResolver, ResolverInterface $localeResolver, \Magento\Framework\Stdlib\DateTime $dateTime, ScopeConfigInterface $scopeConfig, $scopeType, $defaultTimezonePath)
 
 getDefaultTimezonePath ()
 
 getDefaultTimezone ()
 
 getConfigTimezone ($scopeType=null, $scopeCode=null)
 
 getDateFormat ($type=\IntlDateFormatter::SHORT)
 
 getDateFormatWithLongYear ()
 
 getTimeFormat ($type=\IntlDateFormatter::SHORT)
 
 getDateTimeFormat ($type)
 
 date ($date=null, $locale=null, $useTimezone=true, $includeTime=true)
 
 scopeDate ($scope=null, $date=null, $includeTime=false)
 
 formatDate ($date=null, $format=\IntlDateFormatter::SHORT, $showTime=false)
 
 scopeTimeStamp ($scope=null)
 
 isScopeDateInInterval ($scope, $dateFrom=null, $dateTo=null)
 
 formatDateTime ( $date, $dateType=\IntlDateFormatter::SHORT, $timeType=\IntlDateFormatter::SHORT, $locale=null, $timezone=null, $pattern=null)
 
 convertConfigTimeToUtc ($date, $format='Y-m-d H:i:s')
 

Protected Attributes

 $_allowedFormats
 
 $_scopeType
 
 $_scopeResolver
 
 $_dateTime
 
 $_defaultTimezonePath
 
 $_scopeConfig
 
 $_localeResolver
 

Detailed Description

Timezone library

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 21 of file Timezone.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ScopeResolverInterface  $scopeResolver,
ResolverInterface  $localeResolver,
\Magento\Framework\Stdlib\DateTime  $dateTime,
ScopeConfigInterface  $scopeConfig,
  $scopeType,
  $defaultTimezonePath 
)
Parameters
ScopeResolverInterface$scopeResolver
ResolverInterface$localeResolver
\Magento\Framework\Stdlib\DateTime$dateTime
ScopeConfigInterface$scopeConfig
string$scopeType
string$defaultTimezonePath

Definition at line 71 of file Timezone.php.

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  }
$dateTime

Member Function Documentation

◆ convertConfigTimeToUtc()

convertConfigTimeToUtc (   $date,
  $format = 'Y-m-d H:i:s' 
)

Convert date from config timezone to Utc. If pass \DateTime object as argument be sure that timezone is the same with config timezone

Parameters
string | \DateTimeInterface$date
string$format
Exceptions
LocalizedException
Returns
string

Implements TimezoneInterface.

Definition at line 310 of file Timezone.php.

310  :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  }

◆ date()

date (   $date = null,
  $locale = null,
  $useTimezone = true,
  $includeTime = true 
)

{Create \DateTime object for current locale

Parameters
mixed$date
string$locale
bool$useTimezone
bool$includeTime
Returns
\DateTime
}

Implements TimezoneInterface.

Definition at line 162 of file Timezone.php.

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  }
getConfigTimezone($scopeType=null, $scopeCode=null)
Definition: Timezone.php:106

◆ formatDate()

formatDate (   $date = null,
  $format = \IntlDateFormatter::SHORT,
  $showTime = false 
)

{Format date using current locale options and time zone.

Parameters
\DateTime | null$date
int$format
bool$showTime
Returns
string
}

Implements TimezoneInterface.

Definition at line 209 of file Timezone.php.

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  }
formatDateTime( $date, $dateType=\IntlDateFormatter::SHORT, $timeType=\IntlDateFormatter::SHORT, $locale=null, $timezone=null, $pattern=null)
Definition: Timezone.php:268
$format
Definition: list.phtml:12

◆ formatDateTime()

formatDateTime (   $date,
  $dateType = \IntlDateFormatter::SHORT,
  $timeType = \IntlDateFormatter::SHORT,
  $locale = null,
  $timezone = null,
  $pattern = null 
)
Parameters
string | \DateTimeInterface$date
int$dateType
int$timeType
string | null$locale
string | null$timezone
string | null$pattern
Returns
string

Implements TimezoneInterface.

Definition at line 268 of file Timezone.php.

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  }
$pattern
Definition: website.php:22
getConfigTimezone($scopeType=null, $scopeCode=null)
Definition: Timezone.php:106

◆ getConfigTimezone()

getConfigTimezone (   $scopeType = null,
  $scopeCode = null 
)

{Gets the scope config timezone

Parameters
string$scopeType
string$scopeCode
Returns
string
}

Implements TimezoneInterface.

Definition at line 106 of file Timezone.php.

107  {
108  return $this->_scopeConfig->getValue(
109  $this->getDefaultTimezonePath(),
110  $scopeType ?: $this->_scopeType,
111  $scopeCode
112  );
113  }

◆ getDateFormat()

getDateFormat (   $type = \IntlDateFormatter::SHORT)

{Retrieve ISO date format

Parameters
int$type
Returns
string
}

Implements TimezoneInterface.

Definition at line 118 of file Timezone.php.

119  {
120  return (new \IntlDateFormatter(
121  $this->_localeResolver->getLocale(),
122  $type,
123  \IntlDateFormatter::NONE
124  ))->getPattern();
125  }
$type
Definition: item.phtml:13

◆ getDateFormatWithLongYear()

getDateFormatWithLongYear ( )

{Retrieve short date format with 4-digit year

Returns
string
}

Implements TimezoneInterface.

Definition at line 130 of file Timezone.php.

131  {
132  return preg_replace(
133  '/(?<!y)yy(?!y)/',
134  'Y',
135  $this->getDateFormat()
136  );
137  }
getDateFormat($type=\IntlDateFormatter::SHORT)
Definition: Timezone.php:118

◆ getDateTimeFormat()

getDateTimeFormat (   $type)

{Retrieve ISO datetime format

Parameters
string$type
Returns
string
}

Implements TimezoneInterface.

Definition at line 154 of file Timezone.php.

155  {
156  return $this->getDateFormat($type) . ' ' . $this->getTimeFormat($type);
157  }
$type
Definition: item.phtml:13
getDateFormat($type=\IntlDateFormatter::SHORT)
Definition: Timezone.php:118
getTimeFormat($type=\IntlDateFormatter::SHORT)
Definition: Timezone.php:142

◆ getDefaultTimezone()

getDefaultTimezone ( )

{Retrieve timezone code

Returns
string
}

Implements TimezoneInterface.

Definition at line 98 of file Timezone.php.

99  {
100  return 'UTC';
101  }

◆ getDefaultTimezonePath()

getDefaultTimezonePath ( )

{Return path to default timezone

Returns
string
}

Implements TimezoneInterface.

Definition at line 90 of file Timezone.php.

◆ getTimeFormat()

getTimeFormat (   $type = \IntlDateFormatter::SHORT)

{Retrieve ISO time format

Parameters
string$type
Returns
string
}

Implements TimezoneInterface.

Definition at line 142 of file Timezone.php.

143  {
144  return (new \IntlDateFormatter(
145  $this->_localeResolver->getLocale(),
146  \IntlDateFormatter::NONE,
147  $type
148  ))->getPattern();
149  }
$type
Definition: item.phtml:13

◆ isScopeDateInInterval()

isScopeDateInInterval (   $scope,
  $dateFrom = null,
  $dateTo = null 
)

{Checks if current date of the given scope (in the scope timezone) is within the range

Parameters
int | string | \Magento\Framework\App\ScopeInterface$scope
string | null$dateFrom
string | null$dateTo
Returns
bool
}

Implements TimezoneInterface.

Definition at line 236 of file Timezone.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ scopeDate()

scopeDate (   $scope = null,
  $date = null,
  $includeTime = false 
)

{Create \DateTime object with date converted to scope timezone and scope Locale

Parameters
mixed$scopeInformation about scope
string | integer | \DateTime | array | null$datedate in UTC
boolean$includeTimeflag for including time to date
Returns
\DateTime
}

Implements TimezoneInterface.

Definition at line 196 of file Timezone.php.

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  }

◆ scopeTimeStamp()

scopeTimeStamp (   $scope = null)

{Get scope timestamp Timestamp will be built with scope timezone settings

Parameters
mixed$scope
Returns
int
}

Implements TimezoneInterface.

Definition at line 223 of file Timezone.php.

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  }
date($date=null, $locale=null, $useTimezone=true, $includeTime=true)
Definition: Timezone.php:162

Field Documentation

◆ $_allowedFormats

$_allowedFormats
protected
Initial value:
= [
\IntlDateFormatter::FULL,
\IntlDateFormatter::LONG,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::SHORT,
]

Definition at line 26 of file Timezone.php.

◆ $_dateTime

$_dateTime
protected

Definition at line 46 of file Timezone.php.

◆ $_defaultTimezonePath

$_defaultTimezonePath
protected

Definition at line 51 of file Timezone.php.

◆ $_localeResolver

$_localeResolver
protected

Definition at line 61 of file Timezone.php.

◆ $_scopeConfig

$_scopeConfig
protected

Definition at line 56 of file Timezone.php.

◆ $_scopeResolver

$_scopeResolver
protected

Definition at line 41 of file Timezone.php.

◆ $_scopeType

$_scopeType
protected

Definition at line 36 of file Timezone.php.


The documentation for this class was generated from the following file: