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