PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.1
Booking for Appointments and Events Calendar – Amelia v2.1
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Domain / Services / DateTime / DateTimeService.php
ameliabooking / src / Domain / Services / DateTime Last commit date
DateTimeService.php 6 months ago
DateTimeService.php
303 lines
1 <?php
2
3 /**
4 * @copyright © Melograno Ventures. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace AmeliaBooking\Domain\Services\DateTime;
9
10 /**
11 * Class DateTimeService
12 *
13 * @package AmeliaBooking\Domain\Services\DateTime
14 */
15 class DateTimeService
16 {
17 /** @var \DateTimeZone */
18 private static $timeZone;
19
20 /**
21 * @param array $settings
22 */
23 public static function setTimeZone($settings)
24 {
25 if (!self::$timeZone) {
26 if ($settings['timeZoneString']) {
27 self::$timeZone = new \DateTimeZone($settings['timeZoneString']);
28 } elseif ($settings['gmtOffset']) {
29 $hours = (int)$settings['gmtOffset'];
30 $minutes = ($settings['gmtOffset'] - floor($settings['gmtOffset'])) * 60;
31
32 self::$timeZone = new \DateTimeZone(sprintf('%+03d:%02d', $hours, $minutes));
33 } else {
34 self::$timeZone = new \DateTimeZone('UTC');
35 }
36 }
37 }
38
39 /**
40 * @param string $timeZone
41 *
42 * @return \DateTimeZone
43 */
44 public static function createTimeZone($timeZone)
45 {
46 return new \DateTimeZone($timeZone);
47 }
48
49 /**
50 * @return \DateTimeZone
51 */
52 public static function getTimeZone()
53 {
54 return self::$timeZone;
55 }
56
57 /**
58 * Return now date and time object by timezone settings
59 *
60 * @return \DateTime
61 */
62 public static function getNowDateTimeObject()
63 {
64 return new \DateTime('now', self::getTimeZone());
65 }
66
67 /**
68 * Return now date and time string by timezone settings
69 *
70 * @return string
71 */
72 public static function getNowDateTime()
73 {
74 return self::getNowDateTimeObject()->format('Y-m-d H:i:s');
75 }
76
77 /**
78 * @param String $dateTimeString
79 *
80 * @return \DateTime
81 */
82 public static function getCustomDateTimeObject($dateTimeString)
83 {
84 return new \DateTime($dateTimeString ? $dateTimeString : 'now', self::getTimeZone());
85 }
86
87 /**
88 * Return custom date and time string by timezone settings
89 *
90 * @param String $dateTimeString
91 *
92 * @return string
93 */
94 public static function getCustomDateTime($dateTimeString)
95 {
96 return self::getCustomDateTimeObject($dateTimeString)->format('Y-m-d H:i:s');
97 }
98
99 /**
100 * @param String $dateTimeString
101 * @param int $clientUtcOffset
102 *
103 * @return \DateTime
104 */
105 public static function getClientUtcCustomDateTimeObject($dateTimeString, $clientUtcOffset)
106 {
107 $clientDateTime = new \DateTime($dateTimeString, new \DateTimeZone('UTC'));
108
109 if ($clientUtcOffset > 0) {
110 $clientDateTime->modify("+{$clientUtcOffset} minutes");
111 } elseif ($clientUtcOffset < 0) {
112 $clientDateTime->modify("{$clientUtcOffset} minutes");
113 }
114
115 return $clientDateTime;
116 }
117
118 /**
119 * Return custom date and time string by utc offset
120 *
121 * @param String $dateTimeString
122 * @param int $clientUtcOffset
123 *
124 * @return string
125 */
126 public static function getClientUtcCustomDateTime($dateTimeString, $clientUtcOffset)
127 {
128 return self::getClientUtcCustomDateTimeObject($dateTimeString, $clientUtcOffset)->format('Y-m-d H:i:s');
129 }
130
131 /**
132 * Return now date and time object in UTC
133 *
134 * @return \DateTime
135 */
136 public static function getNowDateTimeObjectInUtc()
137 {
138 return self::getNowDateTimeObject()->setTimezone(new \DateTimeZone('UTC'));
139 }
140
141 /**
142 * Return now date and time string in UTC
143 *
144 * @return string
145 */
146 public static function getNowDateTimeInUtc()
147 {
148 return self::getNowDateTimeObjectInUtc()->format('Y-m-d H:i:s');
149 }
150
151 /**
152 * Return custom date and time object in UTC
153 *
154 * @param $dateTimeString
155 *
156 * @return \DateTime
157 */
158 public static function getCustomDateTimeObjectInUtc($dateTimeString)
159 {
160 return self::getCustomDateTimeObject($dateTimeString)->setTimezone(new \DateTimeZone('UTC'));
161 }
162
163 /**
164 * Return custom date and time object in UTC
165 *
166 * @param $dateTimeString
167 * @param $timezone
168 *
169 * @return \DateTime
170 */
171 public static function getCustomDateTimeObjectInTimeZone($dateTimeString, $timezone)
172 {
173 return self::getCustomDateTimeObject($dateTimeString)->setTimezone(new \DateTimeZone($timezone));
174 }
175
176 /**
177 * Return custom date and time object in UTC
178 *
179 * @param $dateTimeString
180 * @param $timezone
181 *
182 * @return \DateTime
183 */
184 public static function getDateTimeObjectInTimeZone($dateTimeString, $timezone)
185 {
186 return (new \DateTime($dateTimeString, new \DateTimeZone($timezone)));
187 }
188
189 /**
190 * Return custom date and time string in UTC
191 *
192 * @param $dateTimeString
193 *
194 * @return string
195 */
196 public static function getCustomDateTimeInUtc($dateTimeString)
197 {
198 return self::getCustomDateTimeObjectInUtc($dateTimeString)->format('Y-m-d H:i:s');
199 }
200
201 /**
202 * Return custom date and time object from UTC
203 *
204 * @param $dateTimeString
205 *
206 * @return \DateTime
207 */
208 public static function getCustomDateTimeObjectFromUtc($dateTimeString)
209 {
210 return (new \DateTime($dateTimeString, new \DateTimeZone('UTC')))->setTimezone(self::getTimeZone());
211 }
212
213 /**
214 * Return custom date and time string from UTC
215 *
216 * @param $dateTimeString
217 *
218 * @return string
219 */
220 public static function getCustomDateTimeFromUtc($dateTimeString)
221 {
222 return self::getCustomDateTimeObjectFromUtc($dateTimeString)->format('Y-m-d H:i:s');
223 }
224
225 /**
226 * Return custom date and time RFC3339 from UTC
227 *
228 * @param string $dateTimeString
229 *
230 * @return string
231 */
232 public static function getCustomDateTimeRFC3339($dateTimeString)
233 {
234 return self::getCustomDateTimeObjectInUtc($dateTimeString)->format(DATE_RFC3339);
235 }
236
237 /**
238 * Return now date string by timezone settings
239 *
240 * @return string
241 */
242 public static function getNowDate()
243 {
244 return self::getNowDateTimeObject()->format('Y-m-d');
245 }
246
247 /**
248 * Return now time string by timezone settings
249 *
250 * @return string
251 */
252 public static function getNowTime()
253 {
254 return self::getNowDateTimeObject()->format('H:i:s');
255 }
256
257 /**
258 * Return current Unix timestamp
259 *
260 * @return false|int
261 */
262 public static function getNowTimestamp()
263 {
264 return strtotime(self::getNowTime());
265 }
266
267 /**
268 * Return Day Index for passed date string in 'YYYY-MM-DD' format.
269 * Monday index is 1, Sunday index is 7.
270 *
271 * @param $dateString
272 *
273 * @return string
274 */
275 public static function getDayIndex($dateString)
276 {
277 return self::getCustomDateTimeObject($dateString)->format('N');
278 }
279
280 /**
281 * @param array $dateTimes
282 *
283 * @return array
284 */
285 public static function getSortedDateTimeStrings($dateTimes)
286 {
287 usort(
288 $dateTimes,
289 function ($a, $b) {
290 return strtotime($a) - strtotime($b);
291 }
292 );
293
294 return $dateTimes;
295 }
296
297 public static function getDateTimeStringInUtc(string $dateTimeString): string
298 {
299 return self::getCustomDateTimeObject($dateTimeString)
300 ->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i:s');
301 }
302 }
303