| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Classes; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use DateTimeZone; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Datetime class. |
| 14 |
*/ |
| 15 |
class StoreengineDatetime 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 int $utc_offset = 0; |
| 24 |
|
| 25 |
/** |
| 26 |
* Output an ISO 8601 date string in local (WordPress) timezone. |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
public function __toString() { |
| 31 |
return $this->format( DATE_ATOM ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Set UTC offset - this is a fixed offset instead of a timezone. |
| 36 |
* |
| 37 |
* @param int $offset Offset. |
| 38 |
*/ |
| 39 |
public function set_utc_offset( int $offset ) { |
| 40 |
$this->utc_offset = intval( $offset ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get UTC offset if set, or default to the DateTime object's offset. |
| 45 |
*/ |
| 46 |
#[\ReturnTypeWillChange] |
| 47 |
public function getOffset(): int { |
| 48 |
return $this->utc_offset ?: parent::getOffset(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Set timezone. |
| 53 |
* |
| 54 |
* @param DateTimeZone $timezone DateTimeZone instance. |
| 55 |
* |
| 56 |
* @return DateTime |
| 57 |
*/ |
| 58 |
#[\ReturnTypeWillChange] |
| 59 |
public function setTimezone( $timezone ): DateTime { |
| 60 |
$this->utc_offset = 0; |
| 61 |
|
| 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 |
* @return int |
| 69 |
*/ |
| 70 |
#[\ReturnTypeWillChange] |
| 71 |
public function getTimestamp(): int { |
| 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 |
* @return int |
| 79 |
*/ |
| 80 |
public function getOffsetTimestamp(): int { |
| 81 |
return $this->getTimestamp() + $this->getOffset(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Format a date based on the offset timestamp. |
| 86 |
* |
| 87 |
* @param string $format Date format. |
| 88 |
* |
| 89 |
* @return string |
| 90 |
*/ |
| 91 |
public function date( string $format ): string { |
| 92 |
if ( 'mysql' === $format ) { |
| 93 |
$format = 'Y-m-d H:i:s'; |
| 94 |
} |
| 95 |
|
| 96 |
return gmdate( $format, $this->getOffsetTimestamp() ); |
| 97 |
} |
| 98 |
|
| 99 |
public function format( $format ): string { |
| 100 |
if ( 'mysql' === $format ) { |
| 101 |
$format = 'Y-m-d H:i:s'; |
| 102 |
} |
| 103 |
|
| 104 |
return parent::format( $format ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Return a localised date based on offset timestamp. Wrapper for date_i18n function. |
| 109 |
* |
| 110 |
* @param string $format Date format. |
| 111 |
* |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
public function date_i18n( string $format = 'Y-m-d' ): string { |
| 115 |
if ( 'mysql' === $format ) { |
| 116 |
$format = 'Y-m-d H:i:s'; |
| 117 |
} |
| 118 |
|
| 119 |
return date_i18n( $format, $this->getOffsetTimestamp() ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* GMT to local (wp-timezone) conversion. |
| 124 |
* |
| 125 |
* @param string $format datetime output format |
| 126 |
* |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
public function toLocal( string $format ): string { |
| 130 |
if ( 'mysql' === $format ) { |
| 131 |
$format = 'Y-m-d H:i:s'; |
| 132 |
} |
| 133 |
|
| 134 |
return get_date_from_gmt( $this->format( 'Y-m-d H:i:s' ), $format ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Local (wp-timezone) to GMT conversion. |
| 139 |
* |
| 140 |
* @param string $format datetime output format |
| 141 |
* |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
public function toGmt( string $format ): string { |
| 145 |
if ( 'mysql' === $format ) { |
| 146 |
$format = 'Y-m-d H:i:s'; |
| 147 |
} |
| 148 |
|
| 149 |
return get_gmt_from_date( $this->format( 'Y-m-d H:i:s' ), $format ); |
| 150 |
} |
| 151 |
|
| 152 |
public function print_date( string $format = 'd M Y, h:i A (T)' ): void { |
| 153 |
storeengine_print_time( $this, $format ); |
| 154 |
} |
| 155 |
} |
| 156 |
|