Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Calendar.php
Go to the documentation of this file.
1 <?php
7 
9 
19 {
25  protected $_date;
26 
32  protected $encoder;
33 
37  protected $_localeResolver;
38 
48  public function __construct(
49  \Magento\Framework\View\Element\Template\Context $context,
50  \Magento\Framework\Stdlib\DateTime\DateTime $date,
51  \Magento\Framework\Json\EncoderInterface $encoder,
52  \Magento\Framework\Locale\ResolverInterface $localeResolver,
53  array $data = []
54  ) {
55  $this->_date = $date;
56  $this->encoder = $encoder;
57  $this->_localeResolver = $localeResolver;
58  parent::__construct($context, $data);
59  }
60 
66  protected function _toHtml()
67  {
68  $localeData = (new DataBundle())->get($this->_localeResolver->getLocale());
69 
70  // get days names
71  $daysData = $localeData['calendar']['gregorian']['dayNames'];
72  $this->assign(
73  'days',
74  [
75  'wide' => $this->encoder->encode(array_values(iterator_to_array($daysData['format']['wide']))),
76  'abbreviated' => $this->encoder->encode(
77  array_values(iterator_to_array($daysData['format']['abbreviated']))
78  ),
79  ]
80  );
81 
94  $monthsData = $localeData['calendar']['gregorian']['monthNames'];
95  $this->assign(
96  'months',
97  [
98  'wide' => $this->encoder->encode(array_values(iterator_to_array($monthsData['format']['wide']))),
99  'abbreviated' => $this->encoder->encode(
100  array_values(
101  iterator_to_array(
102  null !== $monthsData->get('format')->get('abbreviated')
103  ? $monthsData['format']['abbreviated']
104  : $monthsData['format']['wide']
105  )
106  )
107  ),
108  ]
109  );
110 
111  // get "today" and "week" words
112  $this->assign('today', $this->encoder->encode($localeData['fields']['day']['relative']['0']));
113  $this->assign('week', $this->encoder->encode($localeData['fields']['week']['dn']));
114 
115  // get "am" & "pm" words
116  $this->assign('am', $this->encoder->encode($localeData['calendar']['gregorian']['AmPmMarkers']['0']));
117  $this->assign('pm', $this->encoder->encode($localeData['calendar']['gregorian']['AmPmMarkers']['1']));
118 
119  // get first day of week and weekend days
120  $this->assign(
121  'firstDay',
122  (int)$this->_scopeConfig->getValue(
123  'general/locale/firstday',
124  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
125  )
126  );
127  $this->assign(
128  'weekendDays',
129  $this->encoder->encode(
130  (string)$this->_scopeConfig->getValue(
131  'general/locale/weekend',
132  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
133  )
134  )
135  );
136 
137  // define default format and tooltip format
138  $this->assign(
139  'defaultFormat',
140  $this->encoder->encode(
141  $this->_localeDate->getDateFormat(\IntlDateFormatter::MEDIUM)
142  )
143  );
144  $this->assign(
145  'toolTipFormat',
146  $this->encoder->encode(
147  $this->_localeDate->getDateFormat(\IntlDateFormatter::LONG)
148  )
149  );
150 
151  // get days and months for en_US locale - calendar will parse exactly in this locale
152  $englishMonths = (new DataBundle())->get('en_US')['calendar']['gregorian']['monthNames'];
153  $enUS = new \stdClass();
154  $enUS->m = new \stdClass();
155  $enUS->m->wide = array_values(iterator_to_array($englishMonths['format']['wide']));
156  $enUS->m->abbr = array_values(iterator_to_array($englishMonths['format']['abbreviated']));
157  $this->assign('enUS', $this->encoder->encode($enUS));
158 
159  return parent::_toHtml();
160  }
161 
167  public function getTimezoneOffsetSeconds()
168  {
169  return $this->_date->getGmtOffset();
170  }
171 
178  public function getStoreTimestamp($store = null)
179  {
180  return $this->_localeDate->scopeTimeStamp($store);
181  }
182 
188  public function getYearRange()
189  {
190  return (new \DateTime())->modify('- 100 years')->format('Y')
191  . ':' . (new \DateTime())->modify('+ 100 years')->format('Y');
192  }
193 }
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Framework\Json\EncoderInterface $encoder, \Magento\Framework\Locale\ResolverInterface $localeResolver, array $data=[])
Definition: Calendar.php:48