2017-03-08 16:51:35 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-07-18 10:46:07 +00:00
|
|
|
* WC Wrapper for PHP DateTime which adds support for gmt/utc offset when a
|
2018-03-21 02:59:56 +00:00
|
|
|
* timezone is absent
|
2017-03-08 16:51:35 +00:00
|
|
|
*
|
2018-03-21 02:59:56 +00:00
|
|
|
* @since 3.0.0
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Classes
|
2018-03-21 02:59:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Datetime class.
|
2017-03-08 16:51:35 +00:00
|
|
|
*/
|
|
|
|
class WC_DateTime extends DateTime {
|
|
|
|
|
2017-03-23 00:11:15 +00:00
|
|
|
/**
|
2017-07-18 10:46:07 +00:00
|
|
|
* UTC Offset, if needed. Only used when a timezone is not set. When
|
|
|
|
* timezones are used this will equal 0.
|
|
|
|
*
|
2017-03-23 00:11:15 +00:00
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $utc_offset = 0;
|
|
|
|
|
2017-03-08 18:14:37 +00:00
|
|
|
/**
|
2017-07-18 10:46:07 +00:00
|
|
|
* Output an ISO 8601 date string in local (WordPress) timezone.
|
2017-03-08 18:14:37 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-03-08 18:14:37 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return $this->format( DATE_ATOM );
|
|
|
|
}
|
|
|
|
|
2017-03-23 00:11:15 +00:00
|
|
|
/**
|
2017-07-18 10:46:07 +00:00
|
|
|
* Set UTC offset - this is a fixed offset instead of a timezone.
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2018-03-21 02:59:56 +00:00
|
|
|
* @param int $offset Offset.
|
2017-03-23 00:11:15 +00:00
|
|
|
*/
|
|
|
|
public function set_utc_offset( $offset ) {
|
|
|
|
$this->utc_offset = intval( $offset );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-18 10:46:07 +00:00
|
|
|
* Get UTC offset if set, or default to the DateTime object's offset.
|
2017-03-23 00:11:15 +00:00
|
|
|
*/
|
|
|
|
public function getOffset() {
|
2018-06-02 17:47:05 +00:00
|
|
|
return $this->utc_offset ? $this->utc_offset : parent::getOffset();
|
2017-03-23 00:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set timezone.
|
2017-05-12 08:48:46 +00:00
|
|
|
*
|
2018-03-21 02:59:56 +00:00
|
|
|
* @param DateTimeZone $timezone DateTimeZone instance.
|
2017-05-15 11:50:52 +00:00
|
|
|
* @return DateTime
|
2017-03-23 00:11:15 +00:00
|
|
|
*/
|
|
|
|
public function setTimezone( $timezone ) {
|
|
|
|
$this->utc_offset = 0;
|
|
|
|
return parent::setTimezone( $timezone );
|
|
|
|
}
|
|
|
|
|
2017-03-08 16:51:35 +00:00
|
|
|
/**
|
2017-07-18 10:46:07 +00:00
|
|
|
* Missing in PHP 5.2 so just here so it can be supported consistently.
|
2017-03-08 18:14:37 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-03-08 16:51:35 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getTimestamp() {
|
|
|
|
return method_exists( 'DateTime', 'getTimestamp' ) ? parent::getTimestamp() : $this->format( 'U' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the timestamp with the WordPress timezone offset added or subtracted.
|
2017-03-08 18:14:37 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-03-08 16:51:35 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getOffsetTimestamp() {
|
|
|
|
return $this->getTimestamp() + $this->getOffset();
|
|
|
|
}
|
2017-03-09 11:06:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a date based on the offset timestamp.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-21 02:59:56 +00:00
|
|
|
* @param string $format Date format.
|
2017-03-09 11:06:36 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function date( $format ) {
|
|
|
|
return gmdate( $format, $this->getOffsetTimestamp() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a localised date based on offset timestamp. Wrapper for date_i18n function.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-21 02:59:56 +00:00
|
|
|
* @param string $format Date format.
|
2017-03-09 11:06:36 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function date_i18n( $format = 'Y-m-d' ) {
|
|
|
|
return date_i18n( $format, $this->getOffsetTimestamp() );
|
|
|
|
}
|
2017-03-08 16:51:35 +00:00
|
|
|
}
|