The human_time_diff WordPress PHP Filter allows you to modify the human-readable difference between two timestamps.
Usage
add_filter('human_time_diff', 'your_custom_function', 10, 4);
function your_custom_function($since, $diff, $from, $to) {
// your custom code here
return $since;
}
Parameters
$since(string): The difference in human-readable text.$diff(int): The difference in seconds.$from(int): Unix timestamp from which the difference begins.$to(int): Unix timestamp to end the time difference.
More information
See WordPress Developer Resources: human_time_diff
Examples
Customizing the Human Time Difference
Customize the human time difference display to show “Just now” for differences less than a minute.
function custom_human_time_diff($since, $diff, $from, $to) {
if ($diff < 60) {
return 'Just now';
}
return $since;
}
add_filter('human_time_diff', 'custom_human_time_diff', 10, 4);
Adding Suffix to Time Difference
Add “ago” as a suffix to the human-readable time difference.
function add_ago_suffix($since, $diff, $from, $to) {
return $since . ' ago';
}
add_filter('human_time_diff', 'add_ago_suffix', 10, 4);
Changing Time Units
Change the time units from the default to only display the difference in hours.
function display_hours_only($since, $diff, $from, $to) {
$hours = round($diff / 3600);
return $hours . ' hours';
}
add_filter('human_time_diff', 'display_hours_only', 10, 4);
Displaying Exact Time Difference
Display the exact time difference in minutes and seconds.
function display_exact_difference($since, $diff, $from, $to) {
$minutes = floor($diff / 60);
$seconds = $diff % 60;
return $minutes . ' minutes ' . $seconds . ' seconds';
}
add_filter('human_time_diff', 'display_exact_difference', 10, 4);
Custom Time Units
Display custom time units like “fortnights” for a 2-week difference.
function custom_time_units($since, $diff, $from, $to) {
if ($diff >= 1209600) {
$fortnights = floor($diff / 1209600);
return $fortnights . ' fortnights';
}
return $since;
}
add_filter('human_time_diff', 'custom_time_units', 10, 4);