PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.20
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.20
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Services / DateTime / DateTime.php

DateTime.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.20, at app/Services/DateTime/DateTime.php

153 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services\DateTime;
4
5 use DateTimeInterface;
6 use DateTimeZone;
7
8 class DateTime extends \FluentCart\Framework\Support\DateTime
9 {
10
11 use CanExtractTimeZone;
12
13 /**
14 * Create a new DateTime Object with current GMT/UTC time
15 *
16 * @return static
17 */
18 public static function gmtNow()
19 {
20
21 return static::create('now', new DateTimeZone('UTC'));
22 }
23
24
25 public static function now($tz = null)
26 {
27 return static::create('now', new DateTimeZone('UTC'));
28 }
29
30 /**
31 * Convert any datetime to GMT/UTC
32 *
33 * @param string|DateTimeInterface|null $datetime The datetime to convert (defaults to current instance)
34 * @return static
35 */
36 public static function anyTimeToGmt($datetime = null, $fromTz = null)
37 {
38 if (is_null($datetime)) {
39 $datetime = static::now();
40 return (clone $datetime)->setTimezone(new DateTimeZone('UTC'));
41 }
42
43 // If it's already a Carbon or DateTime instance
44 if ($datetime instanceof \DateTimeInterface) {
45 $dt = clone $datetime;
46
47 // If fromTz is specified and the datetime doesn't have timezone info, set it
48 if ($fromTz && $dt->getTimezone()->getName() === 'UTC' && $fromTz !== 'UTC') {
49 $dt = $dt->setTimezone(new \DateTimeZone($fromTz));
50 }
51
52 return $dt->setTimezone(new \DateTimeZone('UTC'));
53 }
54
55 // If it's a Unix timestamp (int or numeric string)
56 if (is_numeric($datetime)) {
57 $dt = new static('@' . (int)$datetime);
58 return $dt->setTimezone(new \DateTimeZone('UTC'));
59 }
60
61 // If it's a string, parse it with optional timezone context
62 if (is_string($datetime)) {
63 $dt = static::parse($datetime);
64
65 // If fromTz is provided and the parsed string doesn't contain timezone info
66 if ($fromTz && !preg_match('/[+-]\d{2}:?\d{2}$|Z$|[A-Z]{3,4}$/i', trim($datetime))) {
67 // Create a new datetime in the specified timezone
68 $dt = static::createFromFormat('Y-m-d H:i:s', $dt->format('Y-m-d H:i:s'), new \DateTimeZone($fromTz));
69 }
70
71 return $dt->setTimezone(new \DateTimeZone('UTC'));
72 }
73
74 throw new \InvalidArgumentException(esc_html__('Invalid datetime format.', 'fluent-cart'));
75 }
76
77 public static function gmdate($datetime = null, $fromTz = null)
78 {
79 return static::anyTimeToGmt($datetime, $fromTz);
80 }
81
82
83 /**
84 * Convert a GMT/UTC datetime to the given timezone
85 *
86 * @param string|\DateTimeInterface|null $datetime The datetime to convert (defaults to now in UTC)
87 * @param string|null $timezone Target timezone (defaults to site timezone)
88 * @return static
89 */
90 public static function gmtToTimezone($datetime = null, $timezone = null): DateTime
91 {
92 // If not a string or DateTimeZone, fallback to wp_timezone()
93 if (!$timezone instanceof \DateTimeZone) {
94 $timezone = is_string($timezone)
95 ? new \DateTimeZone($timezone)
96 : wp_timezone(); // returns a DateTimeZone object
97 }
98
99
100 if (is_null($datetime)) {
101 $datetime = static::now(); // returns UTC time
102 }
103
104 if ($datetime instanceof \DateTimeInterface) {
105 return (clone $datetime)->setTimezone($timezone);
106 }
107
108 if (is_numeric($datetime)) {
109 $dt = new static('@' . (int)$datetime);
110 return $dt->setTimezone($timezone);
111 }
112
113 if (is_string($datetime)) {
114 $dt = static::parse($datetime, new \DateTimeZone('UTC')); // assume UTC
115 return $dt->setTimezone($timezone);
116 }
117 throw new \InvalidArgumentException(esc_html__('Invalid datetime format.', 'fluent-cart'));
118
119 }
120
121
122 public function getDefaultTimezone()
123 {
124 return new DateTimeZone('UTC');
125 }
126
127
128 public static function parse($datetimeString, $timezone = null)
129 {
130 $hasTimezone = preg_match('/([Zz]|[+-]\d{2}:\d{2}|[+-]\d{4})/', $datetimeString);
131
132 try {
133 if ($hasTimezone) {
134 // Use the timezone embedded in the string
135 $dt = new \DateTimeImmutable($datetimeString);
136 } else {
137 $tz = $timezone instanceof \DateTimeZone
138 ? $timezone
139 : ($timezone ? new \DateTimeZone($timezone) : new DateTimeZone('UTC'));
140
141 $dt = new \DateTimeImmutable($datetimeString, $tz);
142 }
143
144 return new static($dt->format('Y-m-d H:i:s'), $dt->getTimezone());
145 } catch (\Exception $e) {
146 throw new \InvalidArgumentException(
147 esc_html__('Unable to parse datetime:', 'fluent-cart') . ' ' . esc_html($e->getMessage())
148 );
149 }
150 }
151
152 }
153