Determines the difference between two timestamps.
The difference is returned in a human readable format such as “1 hour”, “5 mins”, “2 days”.
Signature
human_time_diff( $from, $to = '' )
- from
- (int) Unix timestamp from which the difference begins.
- to
- (int) Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
Default:''
Return
(string) Human readable time difference.
Source
function human_time_diff( $from, $to = '' ) {
if ( empty( $to ) )
$to = time();
$diff = (int) abs( $to - $from );
if ( $diff <= HOUR_IN_SECONDS ) {
$mins = round( $diff / MINUTE_IN_SECONDS );
if ( $mins <= 1 ) {
$mins = 1;
}
/* translators: min=minute */
$since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
} elseif ( ( $diff <= DAY_IN_SECONDS ) && ( $diff > HOUR_IN_SECONDS ) ) {
$hours = round( $diff / HOUR_IN_SECONDS );
if ( $hours <= 1 ) {
$hours = 1;
}
$since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
} elseif ( $diff >= DAY_IN_SECONDS ) {
$days = round( $diff / DAY_IN_SECONDS );
if ( $days <= 1 ) {
$days = 1;
}
$since = sprintf( _n( '%s day', '%s days', $days ), $days );
}
return $since;
}
WP Trac GitHub Bitbucket
Link here
-
URL
http://queryposts.com/function/human_time_diff/ -
HTML
<a href='http://queryposts.com/function/human_time_diff/'>human_time_diff()</a> -
Markdown
[human_time_diff()](http://queryposts.com/function/human_time_diff/) -
BBCode
[url=http://queryposts.com/function/human_time_diff/]human_time_diff()[/url]