Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading