Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DateTimeFormatter.php
Go to the documentation of this file.
1 <?php
8 
11 
16 {
21 
25  private $localeResolver;
26 
30  public function __construct(
32  ) {
33  $this->useIntlFormatObject = (null === $useIntlFormatObject)
34  ? !defined('HHVM_VERSION')
36  }
37 
43  private function getLocaleResolver()
44  {
45  if ($this->localeResolver === null) {
46  $this->localeResolver = \Magento\Framework\App\ObjectManager::getInstance()->get(
47  \Magento\Framework\Locale\ResolverInterface::class
48  );
49  }
50  return $this->localeResolver;
51  }
52 
56  public function formatObject($object, $format = null, $locale = null)
57  {
58  $locale = (null === $locale) ? $this->getLocaleResolver()->getLocale() : $locale;
59  if ($this->useIntlFormatObject) {
60  return \IntlDateFormatter::formatObject($object, $format, $locale);
61  }
62  return $this->doFormatObject($object, $format, $locale);
63  }
64 
74  protected function doFormatObject($object, $format = null, $locale = null)
75  {
76  $pattern = $dateFormat = $timeFormat = $calendar = null;
77 
78  if (is_array($format)) {
79  list($dateFormat, $timeFormat) = $format;
80  } elseif (is_numeric($format)) {
81  $dateFormat = $format;
82  } elseif (is_string($format) || null == $format) {
83  $dateFormat = $timeFormat = \IntlDateFormatter::MEDIUM;
84  $pattern = $format;
85  } else {
86  throw new LocalizedException(
87  new Phrase('The format type is invalid. Verify the format type and try again.')
88  );
89  }
90 
91  $timezone = $object->getTimezone();
92  if ($object instanceof \IntlCalendar) {
93  $timezone = $timezone->toDateTimeZone();
94  }
95  $timezone = $timezone->getName();
96 
97  if ($timezone === '+00:00') {
98  $timezone = 'UTC';
99  } elseif ($timezone[0] === '+' || $timezone[0] === '-') { // $timezone[0] is first symbol of string
100  $timezone = 'GMT' . $timezone;
101  }
102 
103  return (new \IntlDateFormatter($locale, $dateFormat, $timeFormat, $timezone, $calendar, $pattern))
104  ->format($object);
105  }
106 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$pattern
Definition: website.php:22
doFormatObject($object, $format=null, $locale=null)
formatObject($object, $format=null, $locale=null)
$format
Definition: list.phtml:12