Advertisement
Guest User

bbPress Freshness Short

a guest
Feb 12th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. add_filter('bbp_time_since', 'bbp_time_short');
  2. add_filter('bbp_get_time_since', 'bbp_get_time_short');
  3.  
  4. function bbp_time_short( $older_date, $newer_date = false ) {
  5. echo bbp_get_time_short( $older_date, $newer_date = false );
  6. }
  7.  
  8. function bbp_get_time_short( $older_date, $newer_date = false ) {
  9.  
  10. // Setup the strings
  11. $unknown_text = apply_filters( 'bbp_core_time_since_unknown_text', __( 'sometime', 'bbpress' ) );
  12. $right_now_text = apply_filters( 'bbp_core_time_since_right_now_text', __( 'right now', 'bbpress' ) );
  13. $ago_text = apply_filters( 'bbp_core_time_since_ago_text', __( '%s ago', 'bbpress' ) );
  14.  
  15. // array of time period chunks
  16. $chunks = array(
  17. array( 60 * 60 * 24 * 365 , __( 'year', 'bbpress' ), __( 'years', 'bbpress' ) ),
  18. array( 60 * 60 * 24 * 30 , __( 'month', 'bbpress' ), __( 'months', 'bbpress' ) ),
  19. array( 60 * 60 * 24 * 7, __( 'week', 'bbpress' ), __( 'weeks', 'bbpress' ) ),
  20. array( 60 * 60 * 24 , __( 'day', 'bbpress' ), __( 'days', 'bbpress' ) ),
  21. array( 60 * 60 , __( 'hour', 'bbpress' ), __( 'hours', 'bbpress' ) ),
  22. array( 60 , __( 'minute', 'bbpress' ), __( 'minutes', 'bbpress' ) ),
  23. array( 1, __( 'second', 'bbpress' ), __( 'seconds', 'bbpress' ) )
  24. );
  25.  
  26. if ( !empty( $older_date ) && !is_numeric( $older_date ) ) {
  27. $time_chunks = explode( ':', str_replace( ' ', ':', $older_date ) );
  28. $date_chunks = explode( '-', str_replace( ' ', '-', $older_date ) );
  29. $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] );
  30. }
  31.  
  32. // $newer_date will equal false if we want to know the time elapsed
  33. // between a date and the current time. $newer_date will have a value if
  34. // we want to work out time elapsed between two known dates.
  35. $newer_date = ( !$newer_date ) ? strtotime( current_time( 'mysql' ) ) : $newer_date;
  36.  
  37. // Difference in seconds
  38. $since = $newer_date - $older_date;
  39.  
  40. // Something went wrong with date calculation and we ended up with a negative date.
  41. if ( 0 > $since ) {
  42. $output = $unknown_text;
  43.  
  44. // We only want to output two chunks of time here, eg:
  45. // x years, xx months
  46. // x days, xx hours
  47. // so there's only two bits of calculation below:
  48. } else {
  49.  
  50. // Step one: the first chunk
  51. for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) {
  52. $seconds = $chunks[$i][0];
  53.  
  54. // Finding the biggest chunk (if the chunk fits, break)
  55. $count = floor( $since / $seconds );
  56. if ( 0 != $count ) {
  57. break;
  58. }
  59. }
  60.  
  61. // If $i iterates all the way to $j, then the event happened 0 seconds ago
  62. if ( !isset( $chunks[$i] ) ) {
  63. $output = $right_now_text;
  64.  
  65. } else {
  66.  
  67. // Set output var
  68. $output = ( 1 == $count ) ? '1 '. $chunks[$i][1] : $count . ' ' . $chunks[$i][2];
  69.  
  70. // Step two: the second chunk
  71. if ( $i + 2 < $j ) {
  72. $seconds2 = $chunks[$i + 1][0];
  73. $name2 = $chunks[$i + 1][1];
  74. $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 );
  75.  
  76. // Add to output var
  77. if ( 0 != $count2 ) {
  78. $output .= ( 1 == $count2 ) ? _x( ',', 'Separator in time since', 'bbpress' ) . ' 1 '. $name2 : _x( ',', 'Separator in time since', 'bbpress' ) . ' ' . $count2 . ' ' . $chunks[$i + 1][2];
  79. }
  80. }
  81.  
  82. // No output, so happened right now
  83. if ( ! (int) trim( $output ) ) {
  84. $output = $right_now_text;
  85. }
  86. }
  87. }
  88.  
  89. // Append 'ago' to the end of time-since if not 'right now'
  90. if ( $output != $right_now_text ) {
  91. $output = sprintf( $ago_text, $output );
  92. }
  93.  
  94. return apply_filters( 'bbp_get_time_short', $output, $older_date, $newer_date );
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement