woocommerce/includes/class-wc-datetime.php

108 lines
1.9 KiB
PHP
Raw Normal View History

2017-03-08 16:51:35 +00:00
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC Wrapper for PHP DateTime.
*
* @class WC_DateTime
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2017-03-08 16:51:35 +00:00
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
class WC_DateTime extends DateTime {
/**
* UTC Offset if needed.
* @var integer
*/
protected $utc_offset = 0;
2017-03-08 18:14:37 +00:00
/**
* Output an ISO 8601 date string in local timezone.
*
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 );
}
/**
* Set UTC offset.
*
* @param int $offset
*/
public function set_utc_offset( $offset ) {
$this->utc_offset = intval( $offset );
}
/**
* getOffset.
*/
public function getOffset() {
if ( $this->utc_offset ) {
return $this->utc_offset;
} else {
return parent::getOffset();
}
}
/**
* Set timezone.
2017-05-12 08:48:46 +00:00
*
* @param DateTimeZone $timezone
*
* @return DateTime
*/
public function setTimezone( $timezone ) {
$this->utc_offset = 0;
return parent::setTimezone( $timezone );
}
2017-03-08 16:51:35 +00:00
/**
* Missing in PHP 5.2.
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
2017-03-09 11:06:36 +00:00
* @param string $format
* @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
2017-03-09 11:06:36 +00:00
* @param string $format
* @return string
*/
public function date_i18n( $format = 'Y-m-d' ) {
return date_i18n( $format, $this->getOffsetTimestamp() );
}
2017-03-08 16:51:35 +00:00
}