PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.1.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.1.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / classes / storeengine-datetime.php

storeengine-datetime.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.1.0, at includes/classes/storeengine-datetime.php

156 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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