Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter('bbp_time_since', 'bbp_time_short');
- add_filter('bbp_get_time_since', 'bbp_get_time_short');
- function bbp_time_short( $older_date, $newer_date = false ) {
- echo bbp_get_time_short( $older_date, $newer_date = false );
- }
- function bbp_get_time_short( $older_date, $newer_date = false ) {
- // Setup the strings
- $unknown_text = apply_filters( 'bbp_core_time_since_unknown_text', __( 'sometime', 'bbpress' ) );
- $right_now_text = apply_filters( 'bbp_core_time_since_right_now_text', __( 'right now', 'bbpress' ) );
- $ago_text = apply_filters( 'bbp_core_time_since_ago_text', __( '%s ago', 'bbpress' ) );
- // array of time period chunks
- $chunks = array(
- array( 60 * 60 * 24 * 365 , __( 'year', 'bbpress' ), __( 'years', 'bbpress' ) ),
- array( 60 * 60 * 24 * 30 , __( 'month', 'bbpress' ), __( 'months', 'bbpress' ) ),
- array( 60 * 60 * 24 * 7, __( 'week', 'bbpress' ), __( 'weeks', 'bbpress' ) ),
- array( 60 * 60 * 24 , __( 'day', 'bbpress' ), __( 'days', 'bbpress' ) ),
- array( 60 * 60 , __( 'hour', 'bbpress' ), __( 'hours', 'bbpress' ) ),
- array( 60 , __( 'minute', 'bbpress' ), __( 'minutes', 'bbpress' ) ),
- array( 1, __( 'second', 'bbpress' ), __( 'seconds', 'bbpress' ) )
- );
- if ( !empty( $older_date ) && !is_numeric( $older_date ) ) {
- $time_chunks = explode( ':', str_replace( ' ', ':', $older_date ) );
- $date_chunks = explode( '-', str_replace( ' ', '-', $older_date ) );
- $older_date = gmmktime( (int) $time_chunks[1], (int) $time_chunks[2], (int) $time_chunks[3], (int) $date_chunks[1], (int) $date_chunks[2], (int) $date_chunks[0] );
- }
- // $newer_date will equal false if we want to know the time elapsed
- // between a date and the current time. $newer_date will have a value if
- // we want to work out time elapsed between two known dates.
- $newer_date = ( !$newer_date ) ? strtotime( current_time( 'mysql' ) ) : $newer_date;
- // Difference in seconds
- $since = $newer_date - $older_date;
- // Something went wrong with date calculation and we ended up with a negative date.
- if ( 0 > $since ) {
- $output = $unknown_text;
- // We only want to output two chunks of time here, eg:
- // x years, xx months
- // x days, xx hours
- // so there's only two bits of calculation below:
- } else {
- // Step one: the first chunk
- for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) {
- $seconds = $chunks[$i][0];
- // Finding the biggest chunk (if the chunk fits, break)
- $count = floor( $since / $seconds );
- if ( 0 != $count ) {
- break;
- }
- }
- // If $i iterates all the way to $j, then the event happened 0 seconds ago
- if ( !isset( $chunks[$i] ) ) {
- $output = $right_now_text;
- } else {
- // Set output var
- $output = ( 1 == $count ) ? '1 '. $chunks[$i][1] : $count . ' ' . $chunks[$i][2];
- // Step two: the second chunk
- if ( $i + 2 < $j ) {
- $seconds2 = $chunks[$i + 1][0];
- $name2 = $chunks[$i + 1][1];
- $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 );
- // Add to output var
- if ( 0 != $count2 ) {
- $output .= ( 1 == $count2 ) ? _x( ',', 'Separator in time since', 'bbpress' ) . ' 1 '. $name2 : _x( ',', 'Separator in time since', 'bbpress' ) . ' ' . $count2 . ' ' . $chunks[$i + 1][2];
- }
- }
- // No output, so happened right now
- if ( ! (int) trim( $output ) ) {
- $output = $right_now_text;
- }
- }
- }
- // Append 'ago' to the end of time-since if not 'right now'
- if ( $output != $right_now_text ) {
- $output = sprintf( $ago_text, $output );
- }
- return apply_filters( 'bbp_get_time_short', $output, $older_date, $newer_date );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement