bbp_get_time_since

Return formatted time to display human readable time difference.

Description

(string) bbp_get_time_since( (string) $older_date, (constant) $newer_date = false, (bool) $gmt = false ); 

Returns (string)

Formatted time

Parameters (3)

0. $older_date (string)
Unix timestamp from which the difference begins.
1. $newer_date — Optional. (constant) => false
Unix timestamp from which the difference ends. False for current time.
2. $gmt — Optional. (bool) => false
Whether to use GMT timezone. Default is false.

Usage

  1. if ( !function_exists( 'bbp_get_time_since' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/common/functions.php'; 
  3.  
  4. // Unix timestamp from which the difference begins. 
  5. $older_date = ''; 
  6.  
  7. // Optional. Unix timestamp from which the 
  8. // difference ends. False for current time. 
  9. $newer_date = false; 
  10.  
  11. // Optional. Whether to use GMT timezone. Default is false. 
  12. $gmt = false; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = bbp_get_time_since($older_date, $newer_date, $gmt); 
  16.  

Defined (1)

The function is defined in the following location(s).

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