Created
January 27, 2017 18:06
-
-
Save adamsilverstein/3865e2faa0bf32585d765c418862904d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function use_floor_for_human_time_diff( $since, $diff, $from, $to ) { | |
if ( empty( $to ) ) { | |
$to = time(); | |
} | |
$diff = (int) abs( $to - $from ); | |
if ( $diff < HOUR_IN_SECONDS ) { | |
$mins = floor( $diff / MINUTE_IN_SECONDS ); | |
if ( $mins <= 1 ) | |
$mins = 1; | |
/* translators: Time difference between two dates, in minutes (min=minute). 1: Number of minutes */ | |
$since = sprintf( _n( '%s min', '%s mins', $mins ), $mins ); | |
} elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) { | |
$hours = floor( $diff / HOUR_IN_SECONDS ); | |
if ( $hours <= 1 ) | |
$hours = 1; | |
/* translators: Time difference between two dates, in hours. 1: Number of hours */ | |
$since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours ); | |
} elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) { | |
$days = floor( $diff / DAY_IN_SECONDS ); | |
if ( $days <= 1 ) | |
$days = 1; | |
/* translators: Time difference between two dates, in days. 1: Number of days */ | |
$since = sprintf( _n( '%s day', '%s days', $days ), $days ); | |
} elseif ( $diff < MONTH_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) { | |
$weeks = floor( $diff / WEEK_IN_SECONDS ); | |
if ( $weeks <= 1 ) | |
$weeks = 1; | |
/* translators: Time difference between two dates, in weeks. 1: Number of weeks */ | |
$since = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks ); | |
} elseif ( $diff < YEAR_IN_SECONDS && $diff >= MONTH_IN_SECONDS ) { | |
$months = floor( $diff / MONTH_IN_SECONDS ); | |
if ( $months <= 1 ) | |
$months = 1; | |
/* translators: Time difference between two dates, in months. 1: Number of months */ | |
$since = sprintf( _n( '%s month', '%s months', $months ), $months ); | |
} elseif ( $diff >= YEAR_IN_SECONDS ) { | |
$years = floor( $diff / YEAR_IN_SECONDS ); | |
if ( $years <= 1 ) | |
$years = 1; | |
/* translators: Time difference between two dates, in years. 1: Number of years */ | |
$since = sprintf( _n( '%s year', '%s years', $years ), $years ); | |
} | |
return $since; | |
} | |
add_filter( 'human_time_diff', 'use_floor_for_human_time_diff', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment