* WC Wrapper for PHP DateTime which adds support for gmt/utc offset when a
* @package WooCommerce\Classes
defined( 'ABSPATH' ) || exit;
class WC_DateTime extends DateTime {
* UTC Offset, if needed. Only used when a timezone is not set. When
* timezones are used this will equal 0.
protected $utc_offset = 0;
* Output an ISO 8601 date string in local (WordPress) timezone.
public function __toString() {
return $this->format( DATE_ATOM );
* Set UTC offset - this is a fixed offset instead of a timezone.
* @param int $offset Offset.
public function set_utc_offset( $offset ) {
$this->utc_offset = intval( $offset );
* Get UTC offset if set, or default to the DateTime object's offset.
public function getOffset() {
return $this->utc_offset ?: parent::getOffset();
* @param DateTimeZone $timezone DateTimeZone instance.
public function setTimezone( $timezone ) {
return parent::setTimezone( $timezone );
* Missing in PHP 5.2 so just here so it can be supported consistently.
public function getTimestamp() {
return method_exists( 'DateTime', 'getTimestamp' ) ? parent::getTimestamp() : $this->format( 'U' );
* Get the timestamp with the WordPress timezone offset added or subtracted.
public function getOffsetTimestamp() {
return $this->getTimestamp() + $this->getOffset();
* Format a date based on the offset timestamp.
* @param string $format Date format.
public function date( $format ) {
return gmdate( $format, $this->getOffsetTimestamp() );
* Return a localised date based on offset timestamp. Wrapper for date_i18n function.
* @param string $format Date format.
public function date_i18n( $format = 'Y-m-d' ) {
return date_i18n( $format, $this->getOffsetTimestamp() );