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