Sachin Sharma Sachin Sharma - 1 month ago 5
MySQL Question

PHP : Not getting correct date difference

I am trying to get difference of two dates to count the duration of time.
This is my code:

<?php
$start_time = '2016-11-02 14:15:02';
$end_time= '2016-11-02 14:17:02';
$diff= strtotime($end_time) - strtotime($start_time);
$duration = date("H:i:s", $diff);
?>


So it is showing 05:32:00, but the actual result should be 00:02:00. I noticed that in result 05:30:00 added. I am not getting solution of it.

Answer

Use gmdate instead of date. It returns formated date string.

$duration = gmdate("H:i:s", $diff);