finding hour difference between two times in php not working

i have 2 times in my sql table like 02:56:07pm and 03:56:14pm, so i converted it like:

$time1 = date("G:i:s", strtotime($row['timein']));
$time2 = date("G:i:s", strtotime($row['timeout']));

which gives me two times like 14:56:07 and 15:56:14

now am trying to find the difference in hours between this two times and i did like below:

$difference = round(abs($time2 - $time1) / 3600,2);
echo $difference;

but here am getting 0 as answer, can anyone please tell me how to fix it, thanks in advance

>Solution :

This works for me:

Using strtotime that converts a date/time string into a Unix Timestamp (eg. 163612416703). Now calculations can be done on the times.

$time1 = strtotime('02:56:07pm');
$time2 = strtotime('05:56:14pm');

$difference = date('H:i:s', ($time2 - $time1));
echo $difference; //03:00:07

Play around with the formatting etc..

Leave a Reply