dingu da
dingu da

Reputation: 23

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

Upvotes: 0

Views: 61

Answers (1)

Bossman
Bossman

Reputation: 1524

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..

Upvotes: 3

Related Questions