WordPress.org

Make WordPress Core

Ticket #28636: 28636.2.diff

File 28636.2.diff, 2.8 KB (added by mboynes, 6 years ago)

Another option for wp_date() and wp_strtotime() which doesn't use date_default_timezone_set()

  • formatting.php

     
    22922292}
    22932293
    22942294/**
     2295 * Returns a formatted date in the local timezone. This is a drop-in
     2296 * replacement for `date()`, except that the returned string will be formatted
     2297 * for the local timezone.
     2298 *
     2299 * If there is a timezone_string available, the date is assumed to be in that
     2300 * timezone, otherwise it simply subtracts the value of the 'gmt_offset'
     2301 * option.
     2302 *
     2303 * @uses get_option() to retrieve the value of 'gmt_offset'.
     2304 * @param string $format The format of the outputted date string.
     2305 * @param string $timestamp Optional. If absent, defaults to `time()`.
     2306 * @return string GMT version of the date provided.
     2307 */
     2308function wp_date( $format, $timestamp = false ) {
     2309        $tz = get_option( 'timezone_string' );
     2310        if ( ! $timestamp ) {
     2311                $timestamp = time();
     2312        }
     2313        if ( $tz ) {
     2314                $date = date_create( "@$timestamp" );
     2315                if ( ! $date ) {
     2316                        return gmdate( $format, 0 );
     2317                }
     2318                $date->setTimezone( new DateTimeZone( $tz ) );
     2319                return $date->format( $format );
     2320        } else {
     2321                return date( $format, $timestamp + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
     2322        }
     2323}
     2324
     2325/**
     2326 * Converts a locally-formatted date to a unix timestamp. This is a drop-in
     2327 * replacement for `strtotime()`, except that where strtotime assumes GMT, this
     2328 * assumes local time (as described below). If a timezone is specified, this
     2329 * function defers to strtotime().
     2330 *
     2331 * If there is a timezone_string available, the date is assumed to be in that
     2332 * timezone, otherwise it simply subtracts the value of the 'gmt_offset'
     2333 * option.
     2334 *
     2335 * @see strtotime()
     2336 * @uses get_option() to retrieve the value of 'gmt_offset'.
     2337 * @param string $string A date/time string. See `strtotime` for valid formats.
     2338 * @return int UNIX timestamp.
     2339 */
     2340function wp_strtotime( $string ) {
     2341        // If there's a timezone specified, we shouldn't convert it
     2342        if ( preg_match( '#(Africa|America|Asia|Australia|Europe|Indian|Pacific)/|UTC|GMT|[-+]\d\d:?\d\d#i', $string ) ) {
     2343                return strtotime( $string );
     2344        }
     2345        $tz_abbr = implode( '|', array_keys( timezone_abbreviations_list() ) );
     2346        if ( preg_match( "/(^|\s)({$tz_abbr})(\s|$)/i", $string ) ) {
     2347                return strtotime( $string );
     2348        }
     2349
     2350        $tz = get_option( 'timezone_string' );
     2351        if ( $tz ) {
     2352                $date = date_create( $string, new DateTimeZone( $tz ) );
     2353                if ( ! $date ) {
     2354                        return strtotime( $string );
     2355                }
     2356                $date->setTimezone( new DateTimeZone( 'UTC' ) );
     2357                return $date->getTimestamp();
     2358        } else {
     2359                return strtotime( $string ) - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
     2360        }
     2361}
     2362
     2363/**
    22952364 * Adds a element attributes to open links in new windows.
    22962365 *
    22972366 * Comment text in popup windows should be filtered through this. Right now it's