How get exact time difference

human_time_diff() only returns a single {number} {unit} string back. It rounds to nearest whole unit, instead of breaking down the difference precisely.

To get an exact duration difference, you’ll need to use your own function. As this is a popular need in PHP, there’s lots of solutions online – here’s a clever one I found:

$t1 = "Jan 08 2018 07:45:14";
$t2 = "Jan 08 2018 09:15:44";

echo time_elapsed_string($t1,$t2);

function time_elapsed_string($datetime,$datetime2, $full = true) {
    $now = new DateTime($datetime2);
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

Which’ll give you:

1 hour, 30 minutes, 30 seconds ago