Cache
2 years ago
DBPermissions.php
2 years ago
DataEncoder.php
2 years ago
Escape.php
2 years ago
Glob.php
2 years ago
Hooks.php
2 years ago
Math.php
2 years ago
PluginInfo.php
2 years ago
Sanitize.php
2 years ago
ServerVars.php
2 years ago
SlashMode.php
5 years ago
Strings.php
2 years ago
Times.php
2 years ago
Urls.php
2 years ago
Version.php
2 years ago
WpDefaultDirectories.php
2 years ago
Times.php
284 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 | |
| 153 | // Three parts: hours, minutes & seconds. |
| 154 | list($second, $minute, $hour) = $duration_parts; |
| 155 | } elseif (2 === $duration_count) { |
| 156 | // Validate ii:ss duration format. |
| 157 | if (!((bool)preg_match('/^([0-5]?[0-9]):([0-5]?[0-9])$/', $duration))) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | // Two parts: minutes & seconds. |
| 162 | list($second, $minute) = $duration_parts; |
| 163 | } else { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | $human_readable_duration = []; |
| 168 | |
| 169 | // Add the hour part to the string. |
| 170 | if (is_numeric($hour)) { |
| 171 | /* translators: %s: Time duration in hour or hours. */ |
| 172 | $human_readable_duration[] = sprintf(_n('%s hour', '%s hours', $hour, 'wp-staging'), (int)$hour); |
| 173 | } |
| 174 | |
| 175 | // Add the minute part to the string. |
| 176 | if (is_numeric($minute)) { |
| 177 | /* translators: %s: Time duration in minute or minutes. */ |
| 178 | $human_readable_duration[] = sprintf(_n('%s minute', '%s minutes', $minute, 'wp-staging'), (int)$minute); |
| 179 | } |
| 180 | |
| 181 | // Add the second part to the string. |
| 182 | if (is_numeric($second)) { |
| 183 | /* translators: %s: Time duration in second or seconds. */ |
| 184 | $human_readable_duration[] = sprintf(_n('%s second', '%s seconds', $second, 'wp-staging'), (int)$second); |
| 185 | } |
| 186 | |
| 187 | return implode(', ', $human_readable_duration); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * |
| 192 | * Alternative to human_time_diff() as it has been changed in WP 5.3 |
| 193 | * Determines the difference between two timestamps. |
| 194 | * |
| 195 | * The difference is returned in a human readable format such as "1 hour", |
| 196 | * "5 mins", "2 days". |
| 197 | * |
| 198 | * @param int $from Unix timestamp from which the difference begins. |
| 199 | * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set. |
| 200 | * @return string Human readable time difference. |
| 201 | * @since 5.3.0 Added support for showing a difference in seconds. |
| 202 | * |
| 203 | * @since 1.5.0 |
| 204 | */ |
| 205 | public function getHumanTimeDiff($from, $to = 0) |
| 206 | { |
| 207 | if (empty($to)) { |
| 208 | $to = time(); |
| 209 | } |
| 210 | |
| 211 | $diff = (int)abs($to - $from); |
| 212 | |
| 213 | if ($diff < MINUTE_IN_SECONDS) { |
| 214 | $secs = $diff; |
| 215 | if ($secs <= 1) { |
| 216 | $secs = 1; |
| 217 | } |
| 218 | |
| 219 | /* translators: Time difference between two dates, in seconds. %s: Number of seconds. */ |
| 220 | $since = sprintf(_n('%s second', '%s seconds', $secs, 'wp-staging'), $secs); |
| 221 | } elseif ($diff < HOUR_IN_SECONDS) { |
| 222 | $mins = round($diff / MINUTE_IN_SECONDS); |
| 223 | if ($mins <= 1) { |
| 224 | $mins = 1; |
| 225 | } |
| 226 | |
| 227 | /* translators: Time difference between two dates, in minutes (min=minute). %s: Number of minutes. */ |
| 228 | $since = sprintf(_n('%s min', '%s mins', $mins, 'wp-staging'), $mins); |
| 229 | } elseif ($diff < DAY_IN_SECONDS) { |
| 230 | $hours = round($diff / HOUR_IN_SECONDS); |
| 231 | if ($hours <= 1) { |
| 232 | $hours = 1; |
| 233 | } |
| 234 | |
| 235 | /* translators: Time difference between two dates, in hours. %s: Number of hours. */ |
| 236 | $since = sprintf(_n('%s hour', '%s hours', $hours, 'wp-staging'), $hours); |
| 237 | } elseif ($diff < WEEK_IN_SECONDS) { |
| 238 | $days = round($diff / DAY_IN_SECONDS); |
| 239 | if ($days <= 1) { |
| 240 | $days = 1; |
| 241 | } |
| 242 | |
| 243 | /* translators: Time difference between two dates, in days. %s: Number of days. */ |
| 244 | $since = sprintf(_n('%s day', '%s days', $days, 'wp-staging'), $days); |
| 245 | } elseif ($diff < MONTH_IN_SECONDS) { |
| 246 | $weeks = round($diff / WEEK_IN_SECONDS); |
| 247 | if ($weeks <= 1) { |
| 248 | $weeks = 1; |
| 249 | } |
| 250 | |
| 251 | /* translators: Time difference between two dates, in weeks. %s: Number of weeks. */ |
| 252 | $since = sprintf(_n('%s week', '%s weeks', $weeks, 'wp-staging'), $weeks); |
| 253 | } elseif ($diff < YEAR_IN_SECONDS) { |
| 254 | $months = round($diff / MONTH_IN_SECONDS); |
| 255 | if ($months <= 1) { |
| 256 | $months = 1; |
| 257 | } |
| 258 | |
| 259 | /* translators: Time difference between two dates, in months. %s: Number of months. */ |
| 260 | $since = sprintf(_n('%s month', '%s months', $months, 'wp-staging'), $months); |
| 261 | } elseif ($diff >= YEAR_IN_SECONDS) { |
| 262 | $years = round($diff / YEAR_IN_SECONDS); |
| 263 | if ($years <= 1) { |
| 264 | $years = 1; |
| 265 | } |
| 266 | |
| 267 | /* translators: Time difference between two dates, in years. %s: Number of years. */ |
| 268 | $since = sprintf(_n('%s year', '%s years', $years, 'wp-staging'), $years); |
| 269 | } |
| 270 | |
| 271 | return $since; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * @return string |
| 276 | * @throws \Exception |
| 277 | */ |
| 278 | public function getCurrentTime() |
| 279 | { |
| 280 | $timeFormatOption = get_option('time_format'); |
| 281 | return (new DateTime('now', $this->getSiteTimezoneObject()))->format($timeFormatOption); |
| 282 | } |
| 283 | } |
| 284 |