WordPress.org

Make WordPress Core

Ticket #28636: 28636.1.diff

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

Adds wp_date(), wp_strtotime(), and wp_get_timezone_string() to simplify working with local dates/times

  • 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        $tzstring = wp_get_timezone_string();
     2310
     2311        // Set to local timezone temporarily
     2312        date_default_timezone_set( $tzstring );
     2313        if ( ! $timestamp ) {
     2314                $timestamp = time();
     2315        }
     2316        $date = date( $format, $timestamp );
     2317        date_default_timezone_set( 'UTC' );
     2318
     2319        return $date;
     2320}
     2321
     2322/**
     2323 * Converts a locally-formatted date to a unix timestamp. This is a drop-in
     2324 * replacement for `strtotime()`, except that where strtotime assumes GMT, this
     2325 * assumes local time (as described below). If a timezone is specified, this
     2326 * function defers to strtotime().
     2327 *
     2328 * If there is a timezone_string available, the date is assumed to be in that
     2329 * timezone, otherwise it simply subtracts the value of the 'gmt_offset'
     2330 * option.
     2331 *
     2332 * @see strtotime()
     2333 * @uses get_option() to retrieve the value of 'gmt_offset'.
     2334 * @param string $string A date/time string. See `strtotime` for valid formats.
     2335 * @return int UNIX timestamp.
     2336 */
     2337function wp_strtotime( $string ) {
     2338        $tzstring = wp_get_timezone_string();
     2339
     2340        // Set to local timezone temporarily
     2341        date_default_timezone_set( $tzstring );
     2342        $timestamp = strtotime( $string );
     2343        date_default_timezone_set( 'UTC' );
     2344
     2345        return $timestamp;
     2346}
     2347
     2348/**
     2349 * Get the timezone string for the current site.
     2350 *
     2351 * @see http://us3.php.net/manual/en/timezones.php
     2352 *
     2353 * @return string A valid timezone string, e.g. "America/New_York".
     2354 */
     2355function wp_get_timezone_string() {
     2356        $tzstring = get_option( 'timezone_string' );
     2357
     2358        if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists
     2359                $current_offset = get_option( 'gmt_offset' );
     2360                if ( 0 == $current_offset ) {
     2361                        $tzstring = 'UTC+0';
     2362                } elseif ( $current_offset < 0 ) {
     2363                        $tzstring = 'UTC' . $current_offset;
     2364                } else {
     2365                        $tzstring = 'UTC+' . $current_offset;
     2366                }
     2367        }
     2368
     2369        return $tzstring;
     2370}
     2371
     2372/**
    22952373 * Adds a element attributes to open links in new windows.
    22962374 *
    22972375 * Comment text in popup windows should be filtered through this. Right now it's