DateTime.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Stdlib\DateTime;
  7. /**
  8. * Date conversion model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class DateTime
  14. {
  15. /**
  16. * Current config offset in seconds
  17. *
  18. * @var int
  19. */
  20. private $_offset = 0;
  21. /**
  22. * @var TimezoneInterface
  23. */
  24. protected $_localeDate;
  25. /**
  26. * @param TimezoneInterface $localeDate
  27. */
  28. public function __construct(TimezoneInterface $localeDate)
  29. {
  30. $this->_localeDate = $localeDate;
  31. $this->_offset = $this->calculateOffset($this->_localeDate->getConfigTimezone());
  32. }
  33. /**
  34. * Calculates timezone offset
  35. *
  36. * @param string|null $timezone
  37. * @return int offset between timezone and gmt
  38. */
  39. public function calculateOffset($timezone = null)
  40. {
  41. $result = true;
  42. $offset = 0;
  43. if ($timezone !== null) {
  44. $oldZone = @date_default_timezone_get();
  45. $result = date_default_timezone_set($timezone);
  46. }
  47. if ($result === true) {
  48. $offset = (int)date('Z');
  49. }
  50. if ($timezone !== null) {
  51. date_default_timezone_set($oldZone);
  52. }
  53. return $offset;
  54. }
  55. /**
  56. * Forms GMT date
  57. *
  58. * @param string $format
  59. * @param int|string $input date in current timezone
  60. * @return string
  61. */
  62. public function gmtDate($format = null, $input = null)
  63. {
  64. if ($format === null) {
  65. $format = 'Y-m-d H:i:s';
  66. }
  67. $date = $this->gmtTimestamp($input);
  68. if ($date === false) {
  69. return false;
  70. }
  71. $result = date($format, $date);
  72. return $result;
  73. }
  74. /**
  75. * Converts input date into date with timezone offset
  76. * Input date must be in GMT timezone
  77. *
  78. * @param string $format
  79. * @param int|string $input date in GMT timezone
  80. * @return string
  81. */
  82. public function date($format = null, $input = null)
  83. {
  84. if ($format === null) {
  85. $format = 'Y-m-d H:i:s';
  86. }
  87. $result = date($format, $this->timestamp($input));
  88. return $result;
  89. }
  90. /**
  91. * Forms GMT timestamp
  92. *
  93. * @param int|string|\DateTimeInterface $input date in current timezone
  94. * @return int
  95. */
  96. public function gmtTimestamp($input = null)
  97. {
  98. if ($input === null) {
  99. return (int)gmdate('U');
  100. } elseif (is_numeric($input)) {
  101. $result = $input;
  102. } elseif ($input instanceof \DateTimeInterface) {
  103. $result = $input->getTimestamp();
  104. } else {
  105. $result = strtotime($input);
  106. }
  107. if ($result === false) {
  108. // strtotime() unable to parse string (it's not a date or has incorrect format)
  109. return false;
  110. }
  111. $date = $this->_localeDate->date($result);
  112. $timestamp = $date->getTimestamp();
  113. unset($date);
  114. return $timestamp;
  115. }
  116. /**
  117. * Converts input date into timestamp with timezone offset
  118. * Input date must be in GMT timezone
  119. *
  120. * @param int|string $input date in GMT timezone
  121. * @return int
  122. */
  123. public function timestamp($input = null)
  124. {
  125. switch (true) {
  126. case ($input === null):
  127. $result = $this->gmtTimestamp();
  128. break;
  129. case (is_numeric($input)):
  130. $result = $input;
  131. break;
  132. case ($input instanceof \DateTimeInterface):
  133. $result = $input->getTimestamp();
  134. break;
  135. default:
  136. $result = strtotime($input);
  137. }
  138. $date = $this->_localeDate->date($result);
  139. return $date->getTimestamp();
  140. }
  141. /**
  142. * Get current timezone offset in seconds/minutes/hours
  143. *
  144. * @param string $type
  145. * @return int
  146. */
  147. public function getGmtOffset($type = 'seconds')
  148. {
  149. $result = $this->_offset;
  150. switch ($type) {
  151. case 'seconds':
  152. default:
  153. break;
  154. case 'minutes':
  155. $result = $result / 60;
  156. break;
  157. case 'hours':
  158. $result = $result / 60 / 60;
  159. break;
  160. }
  161. return $result;
  162. }
  163. }