Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/18/2011 07:01:49 AM (15 years ago)
Author:
johnjamesjacoby
Message:

Swap '0 seconds ago' out for 'right now.' Includes changes to bp_core_time_since() and removes references to 'ago' as needed to avoid possible 'right now ago' condition.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-functions.php

    r4663 r4678  
    598598function bp_core_time_since( $older_date, $newer_date = false ) {
    599599
     600        // Setup the strings
     601        $unknown_text   = apply_filters( 'bp_core_time_since_unknown_text',   __( 'sometime',  'buddypress' ) );
     602        $right_now_text = apply_filters( 'bp_core_time_since_right_now_text', __( 'right now', 'buddypress' ) );
     603        $ago_text       = apply_filters( 'bp_core_time_since_ago_text',       __( '%s ago',    'buddypress' ) );
     604       
    600605        // array of time period chunks
    601606        $chunks = array(
    602                 array( 60 * 60 * 24 * 365 , __( 'year', 'buddypress' ), __( 'years', 'buddypress' ) ),
    603                 array( 60 * 60 * 24 * 30 , __( 'month', 'buddypress' ), __( 'months', 'buddypress' ) ),
    604                 array( 60 * 60 * 24 * 7, __( 'week', 'buddypress' ), __( 'weeks', 'buddypress' ) ),
    605                 array( 60 * 60 * 24 , __( 'day', 'buddypress' ), __( 'days', 'buddypress' ) ),
    606                 array( 60 * 60 , __( 'hour', 'buddypress' ), __( 'hours', 'buddypress' ) ),
    607                 array( 60 , __( 'minute', 'buddypress' ), __( 'minutes', 'buddypress' ) ),
    608                 array( 1, __( 'second', 'buddypress' ), __( 'seconds', 'buddypress' ) )
     607                array( 60 * 60 * 24 * 365 , __( 'year',   'buddypress' ), __( 'years',  'buddypress' ) ),
     608                array( 60 * 60 * 24 * 30 ,  __( 'month',  'buddypress' ), __( 'months', 'buddypress' ) ),
     609                array( 60 * 60 * 24 * 7,    __( 'week',   'buddypress' ), __( 'weeks',  'buddypress' ) ),
     610                array( 60 * 60 * 24 ,       __( 'day',    'buddypress' ), __( 'days',    'buddypress' ) ),
     611                array( 60 * 60 ,            __( 'hour',   'buddypress' ), __( 'hours',  'buddypress' ) ),
     612                array( 60 ,                 __( 'minute', 'buddypress' ), __( 'minutes', 'buddypress' ) ),
     613                array( 1,                   __( 'second', 'buddypress' ), __( 'seconds', 'buddypress' ) )
    609614        );
    610615
    … …  
    626631
    627632        // Something went wrong with date calculation and we ended up with a negative date.
    628         if ( 0 > $since )
    629                 return __( 'sometime', 'buddypress' );
    630 
     633        if ( 0 > $since ) {
     634                $output = $unknown_text;
     635               
    631636        /**
    632637         * We only want to output two chunks of time here, eg:
    … …  
    635640         * so there's only two bits of calculation below:
    636641         */
    637 
    638         // Step one: the first chunk
    639         for ( $i = 0, $j = count($chunks); $i < $j; $i++) {
    640                 $seconds = $chunks[$i][0];
    641 
    642                 // Finding the biggest chunk (if the chunk fits, break)
    643                 if ( ( $count = floor($since / $seconds) ) != 0 )
    644                         break;
    645         }
    646 
    647         // If $i iterates all the way to $j, then the event happened 0 seconds ago
    648         if ( !isset( $chunks[$i] ) )
    649                 return '0 ' . __( 'seconds', 'buddypress' );
    650 
    651         // Set output var
    652         $output = ( 1 == $count ) ? '1 '. $chunks[$i][1] : $count . ' ' . $chunks[$i][2];
    653 
    654         // Step two: the second chunk
    655         if ( $i + 2 < $j ) {
    656                 $seconds2 = $chunks[$i + 1][0];
    657                 $name2 = $chunks[$i + 1][1];
    658 
    659                 if ( ( $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ) ) != 0 ) {
    660                         // Add to output var
    661                         $output .= ( 1 == $count2 ) ? _x( ',', 'Separator in time since', 'buddypress' ) . ' 1 '. $chunks[$i + 1][1] : _x( ',', 'Separator in time since', 'buddypress' ) . ' ' . $count2 . ' ' . $chunks[$i + 1][2];
     642        } else {
     643
     644                // Step one: the first chunk
     645                for ( $i = 0, $j = count($chunks); $i < $j; $i++) {
     646                        $seconds = $chunks[$i][0];
     647
     648                        // Finding the biggest chunk (if the chunk fits, break)
     649                        if ( ( $count = floor($since / $seconds) ) != 0 ) {
     650                                break;
     651                        }
    662652                }
    663         }
    664 
    665         if ( !(int)trim( $output ) )
    666                 $output = '0 ' . __( 'seconds', 'buddypress' );
     653
     654                // If $i iterates all the way to $j, then the event happened 0 seconds ago
     655                if ( !isset( $chunks[$i] ) ) {
     656                        $output = $right_now_text;
     657
     658                } else {
     659
     660                        // Set output var
     661                        $output = ( 1 == $count ) ? '1 '. $chunks[$i][1] : $count . ' ' . $chunks[$i][2];
     662
     663                        // Step two: the second chunk
     664                        if ( $i + 2 < $j ) {
     665                                $seconds2 = $chunks[$i + 1][0];
     666                                $name2 = $chunks[$i + 1][1];
     667
     668                                if ( ( $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ) ) != 0 ) {
     669                                        // Add to output var
     670                                        $output .= ( 1 == $count2 ) ? _x( ',', 'Separator in time since', 'buddypress' ) . ' 1 '. $chunks[$i + 1][1] : _x( ',', 'Separator in time since', 'buddypress' ) . ' ' . $count2 . ' ' . $chunks[$i + 1][2];
     671                                }
     672                        }
     673
     674                        // No output, so happened right now
     675                        if ( !(int)trim( $output ) ) {
     676                                $output = $right_now_text;
     677                        }
     678                }
     679        }
     680
     681        // Append 'ago' to the end of time-since if not 'right now'
     682        if ( $output != $right_now_text ) {
     683                $output = sprintf( $ago_text, $output );
     684        }
    667685
    668686        return $output;
Note: See TracChangeset for help on using the changeset viewer.