WordPress.org

Make WordPress Core

Ticket #28636: 28636.5.diff

File 28636.5.diff, 2.7 KB (added by mboynes, 6 years ago)

Latest update to date functions, removing adjacent @$ to improve readability

  • wp-includes/formatting.php

     
    23272327}
    23282328
    23292329/**
     2330 * Returns a formatted date in the local timezone. This is a drop-in
     2331 * replacement for `date()`, except that the returned string will be formatted
     2332 * for the local timezone.
     2333 *
     2334 * If there is a timezone_string available, the date is assumed to be in that
     2335 * timezone, otherwise it simply subtracts the value of the 'gmt_offset'
     2336 * option.
     2337 *
     2338 * @uses get_option() to retrieve the value of 'gmt_offset'.
     2339 * @param string $format The format of the outputted date string.
     2340 * @param string $timestamp Optional. If absent, defaults to `time()`.
     2341 * @return string GMT version of the date provided.
     2342 */
     2343function wp_date( $format, $timestamp = false ) {
     2344        $tz = get_option( 'timezone_string' );
     2345        if ( ! $timestamp ) {
     2346                $timestamp = time();
     2347        }
     2348        if ( $tz ) {
     2349                $date = date_create( '@' . $timestamp );
     2350                if ( ! $date ) {
     2351                        return gmdate( $format, 0 );
     2352                }
     2353                $date->setTimezone( new DateTimeZone( $tz ) );
     2354                return $date->format( $format );
     2355        } else {
     2356                return date( $format, $timestamp + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
     2357        }
     2358}
     2359
     2360/**
     2361 * Converts a locally-formatted date to a unix timestamp. This is a drop-in
     2362 * replacement for `strtotime()`, except that where strtotime assumes GMT, this
     2363 * assumes local time (as described below). If a timezone is specified, this
     2364 * function defers to strtotime().
     2365 *
     2366 * If there is a timezone_string available, the date is assumed to be in that
     2367 * timezone, otherwise it simply subtracts the value of the 'gmt_offset'
     2368 * option.
     2369 *
     2370 * @see strtotime()
     2371 * @uses get_option() to retrieve the value of 'gmt_offset'.
     2372 * @param string $string A date/time string. See `strtotime` for valid formats.
     2373 * @return int UNIX timestamp.
     2374 */
     2375function wp_strtotime( $string ) {
     2376        // If there's a timezone specified, we shouldn't convert it
     2377        try {
     2378                $test_date = new DateTime( $string );
     2379                if ( 'UTC' != $test_date->getTimezone()->getName() ) {
     2380                        return strtotime( $string );
     2381                }
     2382        } catch ( Exception $e ) {
     2383                return strtotime( $string );
     2384        }
     2385
     2386        $tz = get_option( 'timezone_string' );
     2387        if ( $tz ) {
     2388                $date = date_create( $string, new DateTimeZone( $tz ) );
     2389                if ( ! $date ) {
     2390                        return strtotime( $string );
     2391                }
     2392                $date->setTimezone( new DateTimeZone( 'UTC' ) );
     2393                return $date->getTimestamp();
     2394        } else {
     2395                return strtotime( $string ) - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
     2396        }
     2397}
     2398
     2399/**
    23302400 * Adds a element attributes to open links in new windows.
    23312401 *
    23322402 * Comment text in popup windows should be filtered through this. Right now it's