PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.8.16
VikBooking Hotel Booking Engine & PMS v1.8.16
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
← All changes | libraries/adapter/date/date.php +358 -0 1.6.3 → 1.8.16 View file →
@@ -1,0 +1,358 @@
1 +<?php
2 +/**
3 + * @package VikWP - Libraries
4 + * @subpackage adapter.date
5 + * @author E4J s.r.l.
6 + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
7 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 + * @link https://vikwp.com
9 + */
10 +
11 +// No direct access
12 +defined('ABSPATH') or die('No script kiddies please!');
13 +
14 +/**
15 + * JDate is a class that stores a date and provides logic to manipulate
16 + * and render that date in a variety of formats.
17 + *
18 + * @since 10.0
19 + */
20 +class JDate extends DateTime
21 +{
22 + const DAY_ABBR = "\x021\x03";
23 + const DAY_NAME = "\x022\x03";
24 + const MONTH_ABBR = "\x023\x03";
25 + const MONTH_NAME = "\x024\x03";
26 +
27 + /**
28 + * The format string to be applied when using the __toString() magic method.
29 + *
30 + * @var string
31 + * @since 10.1.19
32 + */
33 + public static $format = 'Y-m-d H:i:s';
34 +
35 + /**
36 + * Placeholder for a DateTimeZone object with GMT as the time zone.
37 + *
38 + * @var object
39 + */
40 + protected static $gmt;
41 +
42 + /**
43 + * Placeholder for a DateTimeZone object with the default server
44 + * time zone as the time zone. This timezone might be altered
45 + * using the `vik_date_default_timezone` hook.
46 + *
47 + * @var object
48 + */
49 + protected static $stz;
50 +
51 + /**
52 + * The default server timezone.
53 + *
54 + * @var string
55 + * @since 10.1.21
56 + */
57 + protected static $default;
58 +
59 + /**
60 + * The DateTimeZone object for usage in reading dates as strings.
61 + *
62 + * @var DateTimeZone
63 + */
64 + protected $tz;
65 +
66 + /**
67 + * Class constructor.
68 + *
69 + * @param string $date String in a format accepted by strtotime(), defaults to "now".
70 + * @param mixed $tz Time zone to be used for the date. Might be a string or a DateTimeZone object.
71 + */
72 + public function __construct($date = 'now', $tz = null)
73 + {
74 + // attempt to initialize static properties
75 + static::getDefaultTimezone();
76 +
77 + // if the time zone object is not set, attempt to build it
78 + if (!($tz instanceof DateTimeZone))
79 + {
80 + if ($tz === null)
81 + {
82 + $tz = self::$gmt;
83 + }
84 + else if (is_string($tz))
85 + {
86 + $tz = new DateTimeZone($tz);
87 + }
88 + }
89 +
90 + // if the date is numeric assume a unix timestamp and convert it
91 + date_default_timezone_set('UTC');
92 + $date = is_numeric($date) ? date('c', $date) : $date;
93 +
94 + // call the DateTime constructor
95 + parent::__construct($date, $tz);
96 +
97 + // reset the timezone for 3rd party libraries/extension that does not use JDate
98 + date_default_timezone_set(self::$stz->getName());
99 +
100 + // Set the timezone object for access later.
101 + $this->tz = $tz;
102 + }
103 +
104 + /**
105 + * Proxy for new JDate().
106 + *
107 + * @param string $date String in a format accepted by strtotime(), defaults to "now".
108 + * @param mixed $tz Time zone to be used for the date.
109 + *
110 + * @return JDate
111 + */
112 + public static function getInstance($date = 'now', $tz = null)
113 + {
114 + return new JDate($date, $tz);
115 + }
116 +
117 + /**
118 + * Method to wrap the setTimezone() function and set the internal time zone object.
119 + *
120 + * @param DateTimeZone $tz The new DateTimeZone object.
121 + *
122 + * @return JDate
123 + *
124 + * @note DO NOT type hint $tz due to a PHP bug: https://bugs.php.net/bug.php?id=61483
125 + */
126 + #[ReturnTypeWillChange]
127 + public function setTimezone($tz)
128 + {
129 + $this->tz = $tz;
130 +
131 + return parent::setTimezone($tz);
132 + }
133 +
134 + /**
135 + * Gets the date as an ISO 8601 string. IETF RFC 3339 defines the ISO 8601 format
136 + * and it can be found at the IETF Web site.
137 + *
138 + * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
139 + *
140 + * @return string The date string in ISO 8601 format.
141 + *
142 + * @link http://www.ietf.org/rfc/rfc3339.txt
143 + *
144 + * @since 10.1.35
145 + */
146 + public function toISO8601($local = false)
147 + {
148 + return $this->format(DateTime::RFC3339, $local, false);
149 + }
150 +
151 + /**
152 + * Gets the date as an SQL datetime string.
153 + *
154 + * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
155 + * @param mixed $db The database driver or null to use JFactory::getDbo().
156 + *
157 + * @return string The date string in SQL datetime format.
158 + *
159 + * @link http://dev.mysql.com/doc/refman/5.0/en/datetime.html
160 + *
161 + * @uses format()
162 + */
163 + public function toSql($local = false, $dbo = null)
164 + {
165 + if ($dbo === null)
166 + {
167 + $dbo = JFactory::getDbo();
168 + }
169 +
170 + return $this->format($dbo->getDateFormat(), $local, false);
171 + }
172 +
173 + /**
174 + * Gets the date as a formatted string.
175 + *
176 + * @param string $format The date format specification string (see {@link PHP_MANUAL#date}).
177 + * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
178 + * @param boolean $translate True to translate localised strings.
179 + *
180 + * @return string The date string in the specified format format.
181 + */
182 + #[ReturnTypeWillChange]
183 + public function format($format, $local = false, $translate = true)
184 + {
185 + /**
186 + * Translate names of days and months.
187 + *
188 + * @since 10.1.28
189 + */
190 + if ($translate)
191 + {
192 + // Do string replacements for date format options that can be translated.
193 + $format = preg_replace('/(^|[^\\\])D/', "\\1" . self::DAY_ABBR, $format);
194 + $format = preg_replace('/(^|[^\\\])l/', "\\1" . self::DAY_NAME, $format);
195 + $format = preg_replace('/(^|[^\\\])M/', "\\1" . self::MONTH_ABBR, $format);
196 + $format = preg_replace('/(^|[^\\\])F/', "\\1" . self::MONTH_NAME, $format);
197 + }
198 +
199 + // if the returned time should not be local use GMT
200 + if ($local == false && !empty(self::$gmt))
201 + {
202 + parent::setTimezone(self::$gmt);
203 + }
204 +
205 + // format the date
206 + $return = parent::format($format);
207 +
208 + if ($translate)
209 + {
210 + // Manually modify the month and day strings in the formatted time.
211 + if (strpos($return, self::DAY_ABBR) !== false)
212 + {
213 + $return = str_replace(self::DAY_ABBR, $this->dayToString(parent::format('w'), true), $return);
214 + }
215 +
216 + if (strpos($return, self::DAY_NAME) !== false)
217 + {
218 + $return = str_replace(self::DAY_NAME, $this->dayToString(parent::format('w')), $return);
219 + }
220 +
221 + if (strpos($return, self::MONTH_ABBR) !== false)
222 + {
223 + $return = str_replace(self::MONTH_ABBR, $this->monthToString(parent::format('n'), true), $return);
224 + }
225 +
226 + if (strpos($return, self::MONTH_NAME) !== false)
227 + {
228 + $return = str_replace(self::MONTH_NAME, $this->monthToString(parent::format('n')), $return);
229 + }
230 + }
231 +
232 + if ($local == false && !empty($this->tz))
233 + {
234 + parent::setTimezone($this->tz);
235 + }
236 +
237 + return $return;
238 + }
239 +
240 + /**
241 + * Translates day of week number to a string.
242 + *
243 + * @param integer $day The numeric day of the week.
244 + * @param boolean $abbr True to return the abbreviated day string.
245 + *
246 + * @return string The day of the week.
247 + *
248 + * @since 10.1.16
249 + */
250 + public function dayToString($day, $abbr = false)
251 + {
252 + switch ($day)
253 + {
254 + case 0:
255 + return $abbr ? __('Sun') : __('Sunday');
256 + case 1:
257 + return $abbr ? __('Mon') : __('Monday');
258 + case 2:
259 + return $abbr ? __('Tue') : __('Tuesday');
260 + case 3:
261 + return $abbr ? __('Wed') : __('Wednesday');
262 + case 4:
263 + return $abbr ? __('Thu') : __('Thursday');
264 + case 5:
265 + return $abbr ? __('Fri') : __('Friday');
266 + case 6:
267 + return $abbr ? __('Sat') : __('Saturday');
268 + }
269 + }
270 +
271 + /**
272 + * Translates month number to a string.
273 + *
274 + * @param integer $month The numeric month of the year.
275 + * @param boolean $abbr If true, return the abbreviated month string
276 + *
277 + * @return string The month of the year.
278 + *
279 + * @since 10.1.28
280 + */
281 + public function monthToString($month, $abbr = false)
282 + {
283 + switch ($month)
284 + {
285 + case 1:
286 + return $abbr ? _x('Jan', 'January abbreviation') : __('January');
287 + case 2:
288 + return $abbr ? _x('Feb', 'February abbreviation') : __('February');
289 + case 3:
290 + return $abbr ? _x('Mar', 'March abbreviation') : __('March');
291 + case 4:
292 + return $abbr ? _x('Apr', 'April abbreviation') : __('April');
293 + case 5:
294 + return $abbr ? _x('May', 'May abbreviation') : __('May');
295 + case 6:
296 + return $abbr ? _x('Jun', 'June abbreviation') : __('June');
297 + case 7:
298 + return $abbr ? _x('Jul', 'July abbreviation') : __('July');
299 + case 8:
300 + return $abbr ? _x('Aug', 'August abbreviation') : __('August');
301 + case 9:
302 + return $abbr ? _x('Sep', 'September abbreviation') : __('September');
303 + case 10:
304 + return $abbr ? _x('Oct', 'October abbreviation') : __('October');
305 + case 11:
306 + return $abbr ? _x('Nov', 'November abbreviation') : __('November');
307 + case 12:
308 + return $abbr ? _x('Dec', 'December abbreviation') : __('December');
309 + }
310 + }
311 +
312 + /**
313 + * Keeps the current standard timezone if not set and returns it.
314 + *
315 + * @return string The standard timezone name.
316 + *
317 + * @since 10.1.3
318 + */
319 + public static function getDefaultTimezone()
320 + {
321 + // create the base GMT and server time zone objects
322 + if (empty(self::$gmt) || empty(self::$stz))
323 + {
324 + self::$default = @date_default_timezone_get();
325 +
326 + /**
327 + * Hook used to set a default timezone, instead of using the one defined by the server.
328 + *
329 + * @param string The timezone string.
330 + *
331 + * @since 10.1.21
332 + */
333 + self::$stz = apply_filters('vik_date_default_timezone', self::$default);
334 +
335 + if (!self::$stz instanceof DateTimeZone)
336 + {
337 + self::$stz = new DateTimeZone(self::$stz);
338 + }
339 +
340 + self::$gmt = new DateTimeZone('GMT');
341 + }
342 +
343 + return self::$default;
344 + }
345 +
346 + /**
347 + * Magic method to render the date object in the format specified in the public
348 + * static member Date::$format.
349 + *
350 + * @return string The date as a formatted string.
351 + *
352 + * @since 10.1.19
353 + */
354 + public function __toString()
355 + {
356 + return (string) parent::format(self::$format);
357 + }
358 +}