Make WordPress Core

Changeset 45853


Ignore:
Timestamp:
08/19/2019 07:07:03 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Date/Time: Introduce wp_timezone_string() and wp_timezone() for unified timezone retrieval.

  • wp_timezone_string() retrieves the timezone from current settings as a string. Uses the timezone_string option to get a proper timezone if available, otherwise falls back to an offset.
  • wp_timezone() retrieves the timezone from current settings as a DateTimeZone object. Timezone can be based on a PHP timezone string or a ±HH:MM offset.

Props Rarst, remcotolsma, johnjamesjacoby, rmccue.
Fixes #24730.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r45754 r45853  
    7575
    7676/**
    77  * Retrieve the date in localized format, based on a sum of Unix timestamp and
     77 * Retrieves the timezone from current settings as a string.
     78 *
     79 * Uses the `timezone_string` option to get a proper timezone if available,
     80 * otherwise falls back to an offset.
     81 *
     82 * @since 5.3.0
     83 *
     84 * @return string PHP timezone string or a ±HH:MM offset.
     85 */
     86function wp_timezone_string() {
     87    $timezone_string = get_option( 'timezone_string' );
     88
     89    if ( $timezone_string ) {
     90        return $timezone_string;
     91    }
     92
     93    $offset  = (float) get_option( 'gmt_offset' );
     94    $hours   = (int) $offset;
     95    $minutes = ( $offset - $hours );
     96
     97    $sign      = ( $offset < 0 ) ? '-' : '+';
     98    $abs_hour  = abs( $hours );
     99    $abs_mins  = abs( $minutes * 60 );
     100    $tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
     101
     102    return $tz_offset;
     103}
     104
     105/**
     106 * Retrieves the timezone from current settings as a `DateTimeZone` object.
     107 *
     108 * Timezone can be based on a PHP timezone string or a ±HH:MM offset.
     109 *
     110 * @since 5.3.0
     111 *
     112 * @return DateTimeZone Timezone object.
     113 */
     114function wp_timezone() {
     115    return new DateTimeZone( wp_timezone_string() );
     116}
     117
     118/**
     119 * Retrieves the date in localized format, based on a sum of Unix timestamp and
    78120 * timezone offset in seconds.
    79121 *
Note: See TracChangeset for help on using the changeset viewer.