bp_core_time_since

Get an English-language representation of the time elapsed since a given date.

Description

(string) bp_core_time_since( (int|string) $older_date, (bool) $newer_date = false ); 

Based on function created by Dunstan Orchard - http://1976design.com

This function will return an English representation of the time elapsed since a given date. eg: 2 hours and 50 minutes eg: 4 days eg: 4 weeks and 6 days

Note that fractions of minutes are not represented in the return string. So an interval of 3 minutes will be represented by 3 minutes ago,, as will an interval of 3 minutes 59 seconds.

Returns (string)

String representing the time since the older date, eg "2 hours and 50 minutes".

Parameters (2)

0. $older_date (int|string)
The earlier time from which you're calculating the time elapsed. Enter either as an integer Unix timestamp, or as a date string of the format Y-m-d h:i:s..
1. $newer_date — Optional. (bool) => false
Unix timestamp of date to compare older date to. Default: false (current time).

Usage

  1. if ( !function_exists( 'bp_core_time_since' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-functions.php'; 
  3. $older_date = null; 
  4.  
  5. // Optional. Unix timestamp of date to compare older 
  6. // date to. Default: false (current time). 
  7. $newer_date = false; 
  8.  
  9. // NOTICE! Understand what this does before running. 
  10. $result = bp_core_time_since($older_date, $newer_date); 
  11.  

Defined (1)

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

/bp-core/bp-core-functions.php  
  1. function bp_core_time_since( $older_date, $newer_date = false ) { 
  2.  
  3. /** 
  4. * Filters whether or not to bypass BuddyPress' time_since calculations. 
  5. * @since 1.7.0 
  6. * @param bool $value Whether or not to bypass. 
  7. * @param string $older_date Earlier time from which we're calculating time elapsed. 
  8. * @param string $newer_date Unix timestamp of date to compare older time to. 
  9. */ 
  10. $pre_value = apply_filters( 'bp_core_time_since_pre', false, $older_date, $newer_date ); 
  11. if ( false !== $pre_value ) { 
  12. return $pre_value; 
  13.  
  14. /** 
  15. * Filters the value to use if the time since is unknown. 
  16. * @since 1.5.0 
  17. * @param string $value String representing the time since the older date. 
  18. */ 
  19. $unknown_text = apply_filters( 'bp_core_time_since_unknown_text', __( 'sometime', buddypress ) ); 
  20.  
  21. /** 
  22. * Filters the value to use if the time since is right now. 
  23. * @since 1.5.0 
  24. * @param string $value String representing the time since the older date. 
  25. */ 
  26. $right_now_text = apply_filters( 'bp_core_time_since_right_now_text', __( 'right now', buddypress ) ); 
  27.  
  28. /** 
  29. * Filters the value to use if the time since is some time ago. 
  30. * @since 1.5.0 
  31. * @param string $value String representing the time since the older date. 
  32. */ 
  33. $ago_text = apply_filters( 'bp_core_time_since_ago_text', __( '%s ago', buddypress ) ); 
  34.  
  35. // Array of time period chunks. 
  36. $chunks = array( 
  37. YEAR_IN_SECONDS,  
  38. 30 * DAY_IN_SECONDS,  
  39. WEEK_IN_SECONDS,  
  40. DAY_IN_SECONDS,  
  41. HOUR_IN_SECONDS,  
  42. MINUTE_IN_SECONDS,  
  43. ); 
  44.  
  45. if ( !empty( $older_date ) && !is_numeric( $older_date ) ) { 
  46. $time_chunks = explode( ':', str_replace( ' ', ':', $older_date ) ); 
  47. $date_chunks = explode( '-', str_replace( ' ', '-', $older_date ) ); 
  48. $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] ); 
  49.  
  50. /** 
  51. * $newer_date will equal false if we want to know the time elapsed between 
  52. * a date and the current time. $newer_date will have a value if we want to 
  53. * work out time elapsed between two known dates. 
  54. */ 
  55. $newer_date = ( !$newer_date ) ? bp_core_current_time( true, 'timestamp' ) : $newer_date; 
  56.  
  57. // Difference in seconds. 
  58. $since = $newer_date - $older_date; 
  59.  
  60. // Something went wrong with date calculation and we ended up with a negative date. 
  61. if ( 0 > $since ) { 
  62. $output = $unknown_text; 
  63.  
  64. /** 
  65. * We only want to output two chunks of time here, eg: 
  66. * x years, xx months 
  67. * x days, xx hours 
  68. * so there's only two bits of calculation below: 
  69. */ 
  70. } else { 
  71.  
  72. // Step one: the first chunk. 
  73. for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) { 
  74. $seconds = $chunks[$i]; 
  75.  
  76. // Finding the biggest chunk (if the chunk fits, break). 
  77. $count = floor( $since / $seconds ); 
  78. if ( 0 != $count ) { 
  79. break; 
  80.  
  81. // If $i iterates all the way to $j, then the event happened 0 seconds ago. 
  82. if ( !isset( $chunks[$i] ) ) { 
  83. $output = $right_now_text; 
  84.  
  85. } else { 
  86.  
  87. // Set output var. 
  88. switch ( $seconds ) { 
  89. case YEAR_IN_SECONDS : 
  90. $output = sprintf( _n( '%s year', '%s years', $count, buddypress ), $count ); 
  91. break; 
  92. case 30 * DAY_IN_SECONDS : 
  93. $output = sprintf( _n( '%s month', '%s months', $count, buddypress ), $count ); 
  94. break; 
  95. case WEEK_IN_SECONDS : 
  96. $output = sprintf( _n( '%s week', '%s weeks', $count, buddypress ), $count ); 
  97. break; 
  98. case DAY_IN_SECONDS : 
  99. $output = sprintf( _n( '%s day', '%s days', $count, buddypress ), $count ); 
  100. break; 
  101. case HOUR_IN_SECONDS : 
  102. $output = sprintf( _n( '%s hour', '%s hours', $count, buddypress ), $count ); 
  103. break; 
  104. case MINUTE_IN_SECONDS : 
  105. $output = sprintf( _n( '%s minute', '%s minutes', $count, buddypress ), $count ); 
  106. break; 
  107. default: 
  108. $output = sprintf( _n( '%s second', '%s seconds', $count, buddypress ), $count ); 
  109.  
  110. // Step two: the second chunk 
  111. // A quirk in the implementation means that this 
  112. // condition fails in the case of minutes and seconds. 
  113. // We've left the quirk in place, since fractions of a 
  114. // minute are not a useful piece of information for our 
  115. // purposes. 
  116. if ( $i + 2 < $j ) { 
  117. $seconds2 = $chunks[$i + 1]; 
  118. $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ); 
  119.  
  120. // Add to output var. 
  121. if ( 0 != $count2 ) { 
  122. $output .= _x( ', ', 'Separator in time since', buddypress ) . ' '; 
  123.  
  124. switch ( $seconds2 ) { 
  125. case 30 * DAY_IN_SECONDS : 
  126. $output .= sprintf( _n( '%s month', '%s months', $count2, buddypress ), $count2 ); 
  127. break; 
  128. case WEEK_IN_SECONDS : 
  129. $output .= sprintf( _n( '%s week', '%s weeks', $count2, buddypress ), $count2 ); 
  130. break; 
  131. case DAY_IN_SECONDS : 
  132. $output .= sprintf( _n( '%s day', '%s days', $count2, buddypress ), $count2 ); 
  133. break; 
  134. case HOUR_IN_SECONDS : 
  135. $output .= sprintf( _n( '%s hour', '%s hours', $count2, buddypress ), $count2 ); 
  136. break; 
  137. case MINUTE_IN_SECONDS : 
  138. $output .= sprintf( _n( '%s minute', '%s minutes', $count2, buddypress ), $count2 ); 
  139. break; 
  140. default: 
  141. $output .= sprintf( _n( '%s second', '%s seconds', $count2, buddypress ), $count2 ); 
  142.  
  143. // No output, so happened right now. 
  144. if ( ! (int) trim( $output ) ) { 
  145. $output = $right_now_text; 
  146.  
  147. // Append 'ago' to the end of time-since if not 'right now'. 
  148. if ( $output != $right_now_text ) { 
  149. $output = sprintf( $ago_text, $output ); 
  150.  
  151. /** 
  152. * Filters the English-language representation of the time elapsed since a given date. 
  153. * @since 1.7.0 
  154. * @param string $output Final 'time since' string. 
  155. * @param string $older_date Earlier time from which we're calculating time elapsed. 
  156. * @param string $newer_date Unix timestamp of date to compare older time to. 
  157. */ 
  158. return apply_filters( 'bp_core_time_since', $output, $older_date, $newer_date );