Modify human_time_diff() to shorten “days” to “d” and “hours” to “h” etc

There is no filter for output of that function. You can fork (copy/rename/edit) it or add wrapper that will replace strings in output like this:

function short_time_diff( $from, $to = '' ) {

    $diff = human_time_diff($from,$to);

    $replace = array(
        'hour'  => 'h',
        'hours' => 'h',
        'day'   => 'd',
        'days'  => 'd',
    );

    return strtr($diff,$replace);
}

PS afterthought – actually strings are localized so there is translation filter to use… But stuff to replace is to generic and that will risk breaking it elsewhere.

UPDATE

Since WP 4.0 there is a filter available for human_time_diff:

add_filter( 'human_time_diff', function($since, $diff, $from, $to) {

    $replace = array(
        'hour'  => 'h',
        'hours' => 'h',
        'day'   => 'd',
        'days'  => 'd',
    );

    return strtr($since, $replace);

}, 10, 4 );

Leave a Comment