Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DateTime.php
Go to the documentation of this file.
1 <?php
8 
15 class DateTime
16 {
22  private $_offset = 0;
23 
27  protected $_localeDate;
28 
32  public function __construct(TimezoneInterface $localeDate)
33  {
34  $this->_localeDate = $localeDate;
35  $this->_offset = $this->calculateOffset($this->_localeDate->getConfigTimezone());
36  }
37 
44  public function calculateOffset($timezone = null)
45  {
46  $result = true;
47  $offset = 0;
48  if ($timezone !== null) {
49  $oldZone = @date_default_timezone_get();
50  $result = date_default_timezone_set($timezone);
51  }
52  if ($result === true) {
53  $offset = (int)date('Z');
54  }
55  if ($timezone !== null) {
56  date_default_timezone_set($oldZone);
57  }
58  return $offset;
59  }
60 
68  public function gmtDate($format = null, $input = null)
69  {
70  if ($format === null) {
71  $format = 'Y-m-d H:i:s';
72  }
73  $date = $this->gmtTimestamp($input);
74  if ($date === false) {
75  return false;
76  }
77  $result = date($format, $date);
78  return $result;
79  }
80 
89  public function date($format = null, $input = null)
90  {
91  if ($format === null) {
92  $format = 'Y-m-d H:i:s';
93  }
94  $result = date($format, $this->timestamp($input));
95  return $result;
96  }
97 
104  public function gmtTimestamp($input = null)
105  {
106  if ($input === null) {
107  return (int)gmdate('U');
108  } elseif (is_numeric($input)) {
109  $result = $input;
110  } elseif ($input instanceof \DateTimeInterface) {
111  $result = $input->getTimestamp();
112  } else {
113  $result = strtotime($input);
114  }
115  if ($result === false) {
116  // strtotime() unable to parse string (it's not a date or has incorrect format)
117  return false;
118  }
119  $date = $this->_localeDate->date($result);
120  $timestamp = $date->getTimestamp();
121  unset($date);
122  return $timestamp;
123  }
124 
132  public function timestamp($input = null)
133  {
134  switch (true) {
135  case ($input === null):
136  $result = $this->gmtTimestamp();
137  break;
138  case (is_numeric($input)):
139  $result = $input;
140  break;
141  case ($input instanceof \DateTimeInterface):
142  $result = $input->getTimestamp();
143  break;
144  default:
145  $result = strtotime($input);
146  }
147 
148  $date = $this->_localeDate->date($result);
149 
150  return $date->getTimestamp();
151  }
152 
159  public function getGmtOffset($type = 'seconds')
160  {
161  $result = $this->_offset;
162  switch ($type) {
163  case 'seconds':
164  default:
165  break;
166  case 'minutes':
167  $result = $result / 60;
168  break;
169  case 'hours':
170  $result = $result / 60 / 60;
171  break;
172  }
173  return $result;
174  }
175 }
gmtDate($format=null, $input=null)
Definition: DateTime.php:68
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$type
Definition: item.phtml:13
$format
Definition: list.phtml:12
__construct(TimezoneInterface $localeDate)
Definition: DateTime.php:32
date($format=null, $input=null)
Definition: DateTime.php:89