Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Date.php
Go to the documentation of this file.
1 <?php
7 
12 
16 class Date extends AbstractDataType
17 {
18  const NAME = 'date';
19 
25  protected $locale;
26 
32  protected $wrappedComponent;
33 
37  private $localeDate;
38 
48  public function __construct(
50  TimezoneInterface $localeDate,
51  ResolverInterface $localeResolver,
52  array $components = [],
53  array $data = []
54  ) {
55  $this->locale = $localeResolver->getLocale();
56  $this->localeDate = $localeDate;
57  parent::__construct($context, $components, $data);
58  }
59 
65  public function prepare()
66  {
67  $config = $this->getData('config');
68 
69  if (!isset($config['storeTimeZone'])) {
70  $storeTimeZone = $this->localeDate->getConfigTimezone();
71  $config['storeTimeZone'] = $storeTimeZone;
72  }
73  // Set date format pattern by current locale
74  $localeDateFormat = $this->localeDate->getDateFormat();
75  $config['options']['dateFormat'] = $localeDateFormat;
76  $config['options']['storeLocale'] = $this->locale;
77  $this->setData('config', $config);
78  parent::prepare();
79  }
80 
86  public function getLocale()
87  {
88  return $this->locale;
89  }
90 
96  public function getComponentName()
97  {
98  return static::NAME;
99  }
100 
111  public function convertDate($date, $hour = 0, $minute = 0, $second = 0, $setUtcTimeZone = true)
112  {
113  try {
114  $dateObj = $this->localeDate->date(
115  new \DateTime(
116  $date,
117  new \DateTimeZone($this->localeDate->getConfigTimezone())
118  ),
119  $this->getLocale(),
120  true
121  );
122  $dateObj->setTime($hour, $minute, $second);
123  //convert store date to default date in UTC timezone without DST
124  if ($setUtcTimeZone) {
125  $dateObj->setTimezone(new \DateTimeZone('UTC'));
126  }
127  return $dateObj;
128  } catch (\Exception $e) {
129  return null;
130  }
131  }
132 }
$config
Definition: fraud_order.php:17
convertDate($date, $hour=0, $minute=0, $second=0, $setUtcTimeZone=true)
Definition: Date.php:111
__construct(ContextInterface $context, TimezoneInterface $localeDate, ResolverInterface $localeResolver, array $components=[], array $data=[])
Definition: Date.php:48