phpdatedate-math

get time difference in hours minutes and seconds


I have two dates,

$expiry_time = strtotime('comming from database');
$current_date = strtotime(date('Y-m-d H:i:s'));
$time_diff = ($expiry_time-$current_date)/(3600);

Here, $time_diff will give me difference in hours. eg. 5.67. But, I want it in hours, minute and second format. How can I achieve this? Thanks.


Solution

  • Use DateTime() with DateInterval()

    $expiry_time = new DateTime($row['fromdb']);
    $current_date = new DateTime();
    $diff = $expiry_time->diff($current_date);
    echo $diff->format('%H:%I:%S');  // returns difference in hr min and sec