current_datetime.php
61 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * `current_datetime()` and `wp_timezone_string()` were added in WordPress 5.3. |
| 5 | * GiveWP currently supports WordPress 5.0, so these functions need to be shimmed. |
| 6 | */ |
| 7 | |
| 8 | if( ! function_exists( 'current_datetime' ) ) |
| 9 | { |
| 10 | /** |
| 11 | * Retrieves the current time as an object using the site’s timezone. |
| 12 | * |
| 13 | * @return DateTimeImmutable|false |
| 14 | */ |
| 15 | function current_datetime() |
| 16 | { |
| 17 | return date_create_immutable('now', wp_timezone()); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | if( ! function_exists( 'wp_timezone' ) ) |
| 22 | { |
| 23 | /** |
| 24 | * Retrieves the timezone of the site as a DateTimeZone object. |
| 25 | * |
| 26 | * @return DateTimeZone |
| 27 | */ |
| 28 | function wp_timezone() |
| 29 | { |
| 30 | return new DateTimeZone(wp_timezone_string()); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | if( ! function_exists( 'wp_timezone_string' ) ) |
| 35 | { |
| 36 | /** |
| 37 | * Retrieves the timezone of the site as a string. |
| 38 | * |
| 39 | * @return mixed|string|void |
| 40 | */ |
| 41 | function wp_timezone_string() |
| 42 | { |
| 43 | $timezone_string = get_option('timezone_string'); |
| 44 | |
| 45 | if ($timezone_string) { |
| 46 | return $timezone_string; |
| 47 | } |
| 48 | |
| 49 | $offset = (float)get_option('gmt_offset'); |
| 50 | $hours = (int)$offset; |
| 51 | $minutes = ($offset - $hours); |
| 52 | |
| 53 | $sign = ($offset < 0) ? '-' : '+'; |
| 54 | $abs_hour = abs($hours); |
| 55 | $abs_mins = abs($minutes * 60); |
| 56 | $tz_offset = sprintf('%s%02d:%02d', $sign, $abs_hour, $abs_mins); |
| 57 | |
| 58 | return $tz_offset; |
| 59 | } |
| 60 | } |
| 61 |