class-wc-datetime.php
| 1 | <?php |
| 2 | /** |
| 3 | * WC Wrapper for PHP DateTime which adds support for gmt/utc offset when a |
| 4 | * timezone is absent |
| 5 | * |
| 6 | * @since 3.0.0 |
| 7 | * @package WooCommerce\Classes |
| 8 | */ |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * Datetime class. |
| 14 | */ |
| 15 | class WC_DateTime extends DateTime { |
| 16 | |
| 17 | /** |
| 18 | * UTC Offset, if needed. Only used when a timezone is not set. When |
| 19 | * timezones are used this will equal 0. |
| 20 | * |
| 21 | * @var integer |
| 22 | */ |
| 23 | protected $utc_offset = 0; |
| 24 | |
| 25 | /** |
| 26 | * Output an ISO 8601 date string in local (WordPress) timezone. |
| 27 | * |
| 28 | * @since 3.0.0 |
| 29 | * @return string |
| 30 | */ |
| 31 | public function __toString() { |
| 32 | return $this->format( DATE_ATOM ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Set UTC offset - this is a fixed offset instead of a timezone. |
| 37 | * |
| 38 | * @param int $offset Offset. |
| 39 | */ |
| 40 | public function set_utc_offset( $offset ) { |
| 41 | $this->utc_offset = intval( $offset ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get UTC offset if set, or default to the DateTime object's offset. |
| 46 | */ |
| 47 | #[\ReturnTypeWillChange] |
| 48 | public function getOffset() { |
| 49 | return $this->utc_offset ?: parent::getOffset(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Set timezone. |
| 54 | * |
| 55 | * @param DateTimeZone $timezone DateTimeZone instance. |
| 56 | * @return DateTime |
| 57 | */ |
| 58 | #[\ReturnTypeWillChange] |
| 59 | public function setTimezone( $timezone ) { |
| 60 | $this->utc_offset = 0; |
| 61 | return parent::setTimezone( $timezone ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Missing in PHP 5.2 so just here so it can be supported consistently. |
| 66 | * |
| 67 | * @since 3.0.0 |
| 68 | * @return int |
| 69 | */ |
| 70 | #[\ReturnTypeWillChange] |
| 71 | public function getTimestamp() { |
| 72 | return method_exists( 'DateTime', 'getTimestamp' ) ? parent::getTimestamp() : $this->format( 'U' ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the timestamp with the WordPress timezone offset added or subtracted. |
| 77 | * |
| 78 | * @since 3.0.0 |
| 79 | * @return int |
| 80 | */ |
| 81 | public function getOffsetTimestamp() { |
| 82 | return $this->getTimestamp() + $this->getOffset(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Format a date based on the offset timestamp. |
| 87 | * |
| 88 | * @since 3.0.0 |
| 89 | * @param string $format Date format. |
| 90 | * @return string |
| 91 | */ |
| 92 | public function date( $format ) { |
| 93 | return gmdate( $format, $this->getOffsetTimestamp() ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Return a localised date based on offset timestamp. Wrapper for date_i18n function. |
| 98 | * |
| 99 | * @since 3.0.0 |
| 100 | * @param string $format Date format. |
| 101 | * @return string |
| 102 | */ |
| 103 | public function date_i18n( $format = 'Y-m-d' ) { |
| 104 | return date_i18n( $format, $this->getOffsetTimestamp() ); |
| 105 | } |
| 106 | } |
| 107 |