/**
* bp_core_time_since()
*
* 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
*
* @package BuddyPress Core
* @param $older_date int Unix timestamp of date you want to calculate the time since for
* @param $newer_date int Unix timestamp of date to compare older date to. Default false (current time).
* @return str The time since.
*/
function bp_core_time_since( $older_date, $newer_date = false ) {
// array of time period chunks
$chunks = array(
array( 60 * 60 * 24 * 365 , __( 'year', 'buddypress' ), __( 'years', 'buddypress' ) ),
array( 60 * 60 * 24 * 30 , __( 'month', 'buddypress' ), __( 'months', 'buddypress' ) ),
array( 60 * 60 * 24 * 7, __( 'week', 'buddypress' ), __( 'weeks', 'buddypress' ) ),
array( 60 * 60 * 24 , __( 'day', 'buddypress' ), __( 'days', 'buddypress' ) ),
array( 60 * 60 , __( 'hour', 'buddypress' ), __( 'hours', 'buddypress' ) ),
array( 60 , __( 'minute', 'buddypress' ), __( 'minutes', 'buddypress' ) ),
array( 1, __( 'second', 'buddypress' ), __( 'seconds', 'buddypress' ) )
);
if ( !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( bp_core_current_time() ) : $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 )
return __( 'sometime', 'buddypress' );
/**
* 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:
*/
/* 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) */
if ( ( $count = floor($since / $seconds) ) != 0 )
break;
}
/* 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];
if ( ( $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ) ) != 0 ) {
/* Add to output var */
$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];
}
}
if ( !(int)trim($output) )
$output = '0 ' . __( 'seconds', 'buddypress' );
return $output;
}