Skip to:
Content

BuddyPress.org

Changeset 9617


Ignore:
Timestamp:
03/11/2015 08:01:21 PM (10 years ago)
Author:
johnjamesjacoby
Message:

Core: Update bp_format_time:

  • Use bp_get_option() over get_blog_option() and (incorrect) get_option() calls.
  • Rename variables for clarity, as generic date/time/offset variables were easy to accidentally stomp.
  • Port logic from wp_timezone_override_offset() and calculate timezone offset if timezone_string is set in the root blog's options.
  • Pass $localize_time parameter into date_i18n() function call, to ensure the most accurate results possible are returned back from WordPress.

See #2693.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-template.php

    r9523 r9617  
    284284 *         'date_format' and 'time_format' settings are). False on failure.
    285285 */
    286 function bp_format_time( $time, $just_date = false, $localize_time = true ) {
    287 
    288     if ( ! isset( $time ) || ! is_numeric( $time ) ) {
     286function bp_format_time( $time = '', $just_date = false, $localize_time = true ) {
     287
     288    // Bail if time is empty or not numeric
     289    // @todo We should output something smarter here
     290    if ( empty( $time ) || ! is_numeric( $time ) ) {
    289291        return false;
    290292    }
    291293
    292294    // Get GMT offset from root blog
    293     $root_blog_offset = false;
    294     if ( ! empty( $localize_time ) ) {
    295         $root_blog_offset = get_blog_option( bp_get_root_blog_id(), 'gmt_offset' );
    296     }
    297 
    298     // Calculate offset time
    299     $time_offset = $time + ( $root_blog_offset * 3600 );
     295    if ( true === $localize_time ) {
     296
     297        // Use Timezone string if set
     298        $timezone_string = bp_get_option( 'timezone_string' );
     299        if ( ! empty( $timezone_string ) ) {
     300            $timezone_object = timezone_open( $timezone_string );
     301            $datetime_object = date_create( "@{$time}" );
     302            $timezone_offset = timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS;
     303
     304        // Fall back on less reliable gmt_offset
     305        } else {
     306            $timezone_offset = bp_get_option( 'gmt_offset' );
     307        }
     308
     309        // Calculate time based on the offset
     310        $calculated_time = $time + ( $timezone_offset * HOUR_IN_SECONDS );
     311
     312    // No localizing, so just use the time that was submitted
     313    } else {
     314        $calculated_time = $time;
     315    }
    300316
    301317    // Current date (January 1, 2010)
    302     $date = date_i18n( get_option( 'date_format' ), $time_offset );
     318    $formatted_date = date_i18n( bp_get_option( 'date_format' ), $calculated_time, $localize_time );
    303319
    304320    // Should we show the time also?
    305     if ( empty( $just_date ) ) {
     321    if ( false === $just_date ) {
     322
    306323        // Current time (9:50pm)
    307         $time = date_i18n( get_option( 'time_format' ), $time_offset );
     324        $formatted_time = date_i18n( bp_get_option( 'time_format' ), $calculated_time, $localize_time );
    308325
    309326        // Return string formatted with date and time
    310         $date = sprintf( __( '%1$s at %2$s', 'buddypress' ), $date, $time );
    311     }
    312 
    313     return apply_filters( 'bp_format_time', $date );
     327        $formatted_date = sprintf( esc_html__( '%1$s at %2$s', 'buddypress' ), $formatted_date, $formatted_time );
     328    }
     329
     330    return apply_filters( 'bp_format_time', $formatted_date );
    314331}
    315332
Note: See TracChangeset for help on using the changeset viewer.