Cache
1 day ago
DBPermissions.php
1 day ago
DataEncoder.php
1 day ago
DatabaseOptions.php
1 day ago
Env.php
1 day ago
Escape.php
1 day ago
Glob.php
1 day ago
Hooks.php
1 day ago
Math.php
1 day ago
PluginInfo.php
1 day ago
Sanitize.php
1 day ago
ServerVars.php
1 day ago
SlashMode.php
1 day ago
Strings.php
1 day ago
Times.php
1 day ago
Urls.php
1 day ago
Version.php
1 day ago
WpDefaultDirectories.php
1 day ago
Times.php
308 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | namespace WPStaging\Framework\Utils; |
| 10 | |
| 11 | use DateInterval; |
| 12 | use DateTime; |
| 13 | use DateTimeImmutable; |
| 14 | use DateTimeZone; |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | class Times |
| 22 | { |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | public function getSiteTimezoneString() |
| 36 | { |
| 37 | |
| 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 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | public function getSiteTimezoneObject() |
| 68 | { |
| 69 | return new DateTimeZone($this->getSiteTimezoneString()); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 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 | |
| 122 | |
| 123 | |
| 124 | |
| 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 | |
| 135 | if ('-' === substr($duration, 0, 1)) { |
| 136 | $duration = substr($duration, 1); |
| 137 | } |
| 138 | |
| 139 | |
| 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 | |
| 149 | if (!((bool)preg_match('/^([0-9]+):([0-5]?[0-9]):([0-5]?[0-9])$/', $duration))) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | list($second, $minute, $hour) = $duration_parts; |
| 155 | } elseif (2 === $duration_count) { |
| 156 | |
| 157 | if (!((bool)preg_match('/^([0-5]?[0-9]):([0-5]?[0-9])$/', $duration))) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | list($second, $minute) = $duration_parts; |
| 163 | } else { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | $human_readable_duration = []; |
| 168 | |
| 169 | |
| 170 | if (is_numeric($hour)) { |
| 171 | /* translators: %s: Time duration in hour or hours. */ |
| 172 | $human_readable_duration[] = sprintf(_n('%s hour', '%s hours', (int)$hour, 'wp-staging'), (int)$hour); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | if (is_numeric($minute)) { |
| 177 | /* translators: %s: Time duration in minute or minutes. */ |
| 178 | $human_readable_duration[] = sprintf(_n('%s minute', '%s minutes', (int)$minute, 'wp-staging'), (int)$minute); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | if (is_numeric($second)) { |
| 183 | /* translators: %s: Time duration in second or seconds. */ |
| 184 | $human_readable_duration[] = sprintf(_n('%s second', '%s seconds', (int)$second, 'wp-staging'), (int)$second); |
| 185 | } |
| 186 | |
| 187 | return implode(', ', $human_readable_duration); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 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 | |
| 276 | |
| 277 | |
| 278 | public function getCurrentTime() |
| 279 | { |
| 280 | $timeFormatOption = get_option('time_format'); |
| 281 | return (new DateTime('now', $this->getSiteTimezoneObject()))->format($timeFormatOption); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | public function getCurrentTimestamp(): int |
| 289 | { |
| 290 | return (new DateTime('now', $this->getSiteTimezoneObject()))->getTimestamp(); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | public static function formatQueryTime(float $seconds): string |
| 300 | { |
| 301 | if ($seconds < 1) { |
| 302 | return round($seconds * 1000, 1) . ' ms'; |
| 303 | } |
| 304 | |
| 305 | return round($seconds, 2) . ' s'; |
| 306 | } |
| 307 | } |
| 308 |