Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
DateTime Class Reference

Public Member Functions

 __construct (TimezoneInterface $localeDate)
 
 calculateOffset ($timezone=null)
 
 gmtDate ($format=null, $input=null)
 
 date ($format=null, $input=null)
 
 gmtTimestamp ($input=null)
 
 timestamp ($input=null)
 
 getGmtOffset ($type='seconds')
 

Protected Attributes

 $_localeDate
 

Detailed Description

Date conversion model

@api

Since
100.0.2

Definition at line 15 of file DateTime.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( TimezoneInterface  $localeDate)
Parameters
TimezoneInterface$localeDate

Definition at line 32 of file DateTime.php.

33  {
34  $this->_localeDate = $localeDate;
35  $this->_offset = $this->calculateOffset($this->_localeDate->getConfigTimezone());
36  }

Member Function Documentation

◆ calculateOffset()

calculateOffset (   $timezone = null)

Calculates timezone offset

Parameters
string | null$timezone
Returns
int offset between timezone and gmt

Definition at line 44 of file DateTime.php.

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  }
date($format=null, $input=null)
Definition: DateTime.php:89

◆ date()

date (   $format = null,
  $input = null 
)

Converts input date into date with timezone offset Input date must be in GMT timezone

Parameters
string$format
int | string$inputdate in GMT timezone
Returns
string

Definition at line 89 of file DateTime.php.

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  }
$format
Definition: list.phtml:12
date($format=null, $input=null)
Definition: DateTime.php:89

◆ getGmtOffset()

getGmtOffset (   $type = 'seconds')

Get current timezone offset in seconds/minutes/hours

Parameters
string$type
Returns
int

Definition at line 159 of file DateTime.php.

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  }
$type
Definition: item.phtml:13

◆ gmtDate()

gmtDate (   $format = null,
  $input = null 
)

Forms GMT date

Parameters
string$format
int | string$inputdate in current timezone
Returns
string

Definition at line 68 of file DateTime.php.

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  }
$format
Definition: list.phtml:12
date($format=null, $input=null)
Definition: DateTime.php:89

◆ gmtTimestamp()

gmtTimestamp (   $input = null)

Forms GMT timestamp

Parameters
int | string | \DateTimeInterface$inputdate in current timezone
Returns
int

Definition at line 104 of file DateTime.php.

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

◆ timestamp()

timestamp (   $input = null)

Converts input date into timestamp with timezone offset Input date must be in GMT timezone

Parameters
int | string$inputdate in GMT timezone
Returns
int

Definition at line 132 of file DateTime.php.

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  }

Field Documentation

◆ $_localeDate

$_localeDate
protected

Definition at line 27 of file DateTime.php.


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