PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 3.0.4
WP STAGING – WordPress Backups, Restore, Migration & Clone v3.0.4
4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Utils / Times.php
wp-staging / Framework / Utils Last commit date
Cache 3 years ago ThirdParty 3 years ago DBPermissions.php 3 years ago Escape.php 3 years ago Math.php 5 years ago Sanitize.php 3 years ago ServerVars.php 3 years ago SlashMode.php 5 years ago Strings.php 3 years ago Times.php 3 years ago Urls.php 3 years ago WpDefaultDirectories.php 3 years ago
Times.php
285 lines
1 <?php
2
3 /**
4 * Handles and manipulates times.
5 *
6 * @package WPStaging\Framework\Utils
7 */
8
9 namespace WPStaging\Framework\Utils;
10
11 use DateInterval;
12 use DateTime;
13 use DateTimeImmutable;
14 use DateTimeZone;
15
16 /**
17 * Class Times
18 *
19 * @package WPStaging\Framework\Utils
20 */
21 class Times
22 {
23
24 /**
25 * Ports wp core wp_timezone_string() function for compatibility with WordPress < 5.3
26 * Retrieves the timezone from site settings as a string.
27 *
28 * Uses the `timezone_string` option to get a proper timezone if available,
29 * otherwise falls back to an offset.
30 *
31 * @return mixed|string|void PHP timezone string or a ±HH:MM offset.
32 * @see wp_timezone_string()
33 *
34 */
35 public function getSiteTimezoneString()
36 {
37 // Early bail: Let's use WordPress core function if it is available.
38 if (function_exists('wp_timezone_string')) {
39 return wp_timezone_string();
40 }
41
42 $timezone_string = get_option('timezone_string');
43
44 if ($timezone_string) {
45 return $timezone_string;
46 }
47
48 $offset = (float)get_option('gmt_offset');
49 $hours = (int)$offset;
50 $minutes = ($offset - $hours);
51
52 $sign = ($offset < 0) ? '-' : '+';
53 $abs_hour = abs($hours);
54 $abs_mins = abs($minutes * 60);
55 $tz_offset = sprintf('%s%02d:%02d', $sign, $abs_hour, $abs_mins);
56
57 return $tz_offset;
58 }
59
60 /**
61 * Retrieves the timezone from site settings as a `DateTimeZone` object.
62 * Timezone can be based on a PHP timezone string or a ±HH:MM offset.
63 * This is copied from wordpress core wp_timezone() which exists since WordPress 5.3.0 for backward compatibility
64 *
65 * @return DateTimeZone Timezone object.
66 */
67 public function getSiteTimezoneObject()
68 {
69 return new DateTimeZone($this->getSiteTimezoneString());
70 }
71
72 /**
73 * Produces a set of date objects modeling a time range.
74 *
75 * This method is similar, in concept, to the PHP Core `range` method
76 * where the entity is changed from numeric values to Dates.
77 *
78 * @param DateTime|DateTimeImmutable|string $start Either a Date object or a valid date definition to start
79 * the range from.
80 * @param DateTime|DateTimeImmutable|string $end Either a Date object or a valid date definition to end
81 * the range at, inclusively.
82 * @param DateInterval|string $step The step definition, as either an Interval object, or as
83 * a valid DateInterval definition.
84 *
85 * @return array<DateTimeImmutable> A list of generated Dates between the start and end.
86 *
87 * @throws \Exception If there's any issue building the start or end date objects from the definitions or building
88 * the interval object from the definition.
89 */
90 public function range($start, $end, $step = 'PT1H')
91 {
92 if ($start instanceof DateTimeImmutable) {
93 $startDateObject = $start;
94 } else {
95 $startDateObject = $start instanceof DateTime ?
96 DateTimeImmutable::createFromMutable($start)
97 : new DateTimeImmutable($start, $this->getSiteTimezoneObject());
98 }
99 if ($end instanceof DateTimeImmutable) {
100 $endDateObject = $end;
101 } else {
102 $endDateObject = $end instanceof DateTime ?
103 DateTimeImmutable::createFromMutable($end)
104 : new DateTimeImmutable($end, $this->getSiteTimezoneObject());
105 }
106 $stepInterval = $step instanceof DateInterval ?
107 $step
108 : new DateInterval($step);
109
110 $values = [];
111 $current = $startDateObject;
112 do {
113 $values[] = $current;
114 $current = $current->add($stepInterval);
115 } while ($current <= $endDateObject);
116
117 return $values;
118 }
119
120 /**
121 * Alternative to human_readable_duration() as it is not available for WP < 5.1
122 * @param string $duration Duration will be in string format (HH:ii:ss) OR (ii:ss),
123 * with a possible prepended negative sign (-).
124 * @return string|false A human readable duration string, false on failure.
125 */
126 public function getHumanReadableDuration($duration)
127 {
128 if ((empty($duration) || !is_string($duration))) {
129 return false;
130 }
131
132 $duration = trim($duration);
133
134 // Remove prepended negative sign.
135 if ('-' === substr($duration, 0, 1)) {
136 $duration = substr($duration, 1);
137 }
138
139 // Extract duration parts.
140 $duration_parts = array_reverse(explode(':', $duration));
141 $duration_count = count($duration_parts);
142
143 $hour = null;
144 $minute = null;
145 $second = null;
146
147 if (3 === $duration_count) {
148 // Validate HH:ii:ss duration format.
149 if (!((bool)preg_match('/^([0-9]+):([0-5]?[0-9]):([0-5]?[0-9])$/', $duration))) {
150 return false;
151 }
152 // Three parts: hours, minutes & seconds.
153 list($second, $minute, $hour) = $duration_parts;
154 } elseif (2 === $duration_count) {
155 // Validate ii:ss duration format.
156 if (!((bool)preg_match('/^([0-5]?[0-9]):([0-5]?[0-9])$/', $duration))) {
157 return false;
158 }
159 // Two parts: minutes & seconds.
160 list($second, $minute) = $duration_parts;
161 } else {
162 return false;
163 }
164
165 $human_readable_duration = [];
166
167 // Add the hour part to the string.
168 if (is_numeric($hour)) {
169 /* translators: %s: Time duration in hour or hours. */
170 $human_readable_duration[] = sprintf(_n('%s hour', '%s hours', $hour, 'wp-staging'), (int)$hour);
171 }
172
173 // Add the minute part to the string.
174 if (is_numeric($minute)) {
175 /* translators: %s: Time duration in minute or minutes. */
176 $human_readable_duration[] = sprintf(_n('%s minute', '%s minutes', $minute, 'wp-staging'), (int)$minute);
177 }
178
179 // Add the second part to the string.
180 if (is_numeric($second)) {
181 /* translators: %s: Time duration in second or seconds. */
182 $human_readable_duration[] = sprintf(_n('%s second', '%s seconds', $second, 'wp-staging'), (int)$second);
183 }
184
185 return implode(', ', $human_readable_duration);
186 }
187
188 /**
189 *
190 * Alternative to human_time_diff() as it has been changed in WP 5.3
191 * Determines the difference between two timestamps.
192 *
193 * The difference is returned in a human readable format such as "1 hour",
194 * "5 mins", "2 days".
195 *
196 * @param int $from Unix timestamp from which the difference begins.
197 * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
198 * @return string Human readable time difference.
199 * @since 5.3.0 Added support for showing a difference in seconds.
200 *
201 * @since 1.5.0
202 */
203 public function getHumanTimeDiff($from, $to = 0)
204 {
205 if (empty($to)) {
206 $to = time();
207 }
208
209 $diff = (int)abs($to - $from);
210
211 if ($diff < MINUTE_IN_SECONDS) {
212 $secs = $diff;
213 if ($secs <= 1) {
214 $secs = 1;
215 }
216 /* translators: Time difference between two dates, in seconds. %s: Number of seconds. */
217 $since = sprintf(_n('%s second', '%s seconds', $secs, 'wp-staging'), $secs);
218 } elseif ($diff < HOUR_IN_SECONDS) {
219 $mins = round($diff / MINUTE_IN_SECONDS);
220 if ($mins <= 1) {
221 $mins = 1;
222 }
223 /* translators: Time difference between two dates, in minutes (min=minute). %s: Number of minutes. */
224 $since = sprintf(_n('%s min', '%s mins', $mins, 'wp-staging'), $mins);
225 } elseif ($diff < DAY_IN_SECONDS) {
226 $hours = round($diff / HOUR_IN_SECONDS);
227 if ($hours <= 1) {
228 $hours = 1;
229 }
230 /* translators: Time difference between two dates, in hours. %s: Number of hours. */
231 $since = sprintf(_n('%s hour', '%s hours', $hours, 'wp-staging'), $hours);
232 } elseif ($diff < WEEK_IN_SECONDS) {
233 $days = round($diff / DAY_IN_SECONDS);
234 if ($days <= 1) {
235 $days = 1;
236 }
237 /* translators: Time difference between two dates, in days. %s: Number of days. */
238 $since = sprintf(_n('%s day', '%s days', $days, 'wp-staging'), $days);
239 } elseif ($diff < MONTH_IN_SECONDS) {
240 $weeks = round($diff / WEEK_IN_SECONDS);
241 if ($weeks <= 1) {
242 $weeks = 1;
243 }
244 /* translators: Time difference between two dates, in weeks. %s: Number of weeks. */
245 $since = sprintf(_n('%s week', '%s weeks', $weeks, 'wp-staging'), $weeks);
246 } elseif ($diff < YEAR_IN_SECONDS) {
247 $months = round($diff / MONTH_IN_SECONDS);
248 if ($months <= 1) {
249 $months = 1;
250 }
251 /* translators: Time difference between two dates, in months. %s: Number of months. */
252 $since = sprintf(_n('%s month', '%s months', $months, 'wp-staging'), $months);
253 } elseif ($diff >= YEAR_IN_SECONDS) {
254 $years = round($diff / YEAR_IN_SECONDS);
255 if ($years <= 1) {
256 $years = 1;
257 }
258 /* translators: Time difference between two dates, in years. %s: Number of years. */
259 $since = sprintf(_n('%s year', '%s years', $years, 'wp-staging'), $years);
260 }
261
262 /**
263 * Filters the human readable difference between two timestamps.
264 *
265 * @param string $since The difference in human readable text.
266 * @param int $diff The difference in seconds.
267 * @param int $from Unix timestamp from which the difference begins.
268 * @param int $to Unix timestamp to end the time difference.
269 * @since 4.0.0
270 *
271 */
272 return apply_filters('human_time_diff', $since, $diff, $from, $to);
273 }
274
275 /**
276 * @return string
277 * @throws \Exception
278 */
279 public function getCurrentTime()
280 {
281 $timeFormatOption = get_option('time_format');
282 return (new DateTime('now', $this->getSiteTimezoneObject()))->format($timeFormatOption);
283 }
284 }
285