Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function human_time_diff( $from, $to = '' )
- {
- $chunks = array(
- array( 60 * 60 * 24 * 365 , '%s year', '%s years' ),
- array( 60 * 60 * 24 * 30 , '%s month', '%s months' ),
- array( 60 * 60 * 24 * 7, '%s week', '%s weeks' ),
- array( 60 * 60 * 24 , '%s day', '%s days' ),
- array( 60 * 60 , '%s hour', '%s hours' ),
- array( 60 , '%s minute', '%s minutes' ),
- array( 1 , '%s second', '%s seconds' ),
- );
- if ( empty( $to ) )
- $to = time();
- $diff = (int) abs( $to - $from );
- for ( $i = 0, $j = count( $chunks ); $i < $j; $i++)
- {
- $seconds = $chunks[$i][0];
- $name1 = $chunks[$i][1];
- $name2 = $chunks[$i][2];
- if ( ( $count = floor( $diff / $seconds ) ) != 0)
- break;
- }
- $since = sprintf( _n( $name1, $name2, $count ), $count );
- $i++;
- if ( $i < $j )
- {
- $seconds_p2 = $chunks[$i][0];
- $name1 = $chunks[$i][1];
- $name2 = $chunks[$i][2];
- if ( ( $count = floor( ( $diff - ( $seconds * $count ) ) / $seconds_p2 ) ) != 0 )
- {
- if( is_rtl() )
- $since = sprintf( _n( $name1, $name2, $count ), $count ) ." ". $since;
- else
- $since = $since. " " . sprintf( _n( $name1, $name2, $count ), $count );
- }
- }
- return $since;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement