Vignesh Gopalakrishnan Vignesh Gopalakrishnan - 2 years ago 132
SQL Question

Getting time difference between two times in PHP

Possible Duplicate:

How to get time difference in minutes in PHP

I am working on an attendance table to calculate the late and very late employees.
I am storing the login time in the table (Type: time). I am able to get the time from the database and i would like to show the time difference in the separate column.

i.e., if employee logged on or before
, then its right time and the time diff should be shown as null. If he logs in after the time i.e,
or later the time difference should be
. Like wise i need to calculate the differences in time.

One time is constant i.e.,
and another one i am getting from database table. Need to get diff between both.
I am working in PHP.
Hope my question is clear.

Thank you in Advance.

Answer Source

You can use strtotime() for time calculation. Here is an example:

$checkTime = strtotime('09:00:59');
echo 'Check Time : '.date('H:i:s', $checkTime);
echo '<hr>';

$loginTime = strtotime('09:01:00');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!'; echo '<br>';
echo 'Time diff in sec: '.abs($diff);

echo '<hr>';

$loginTime = strtotime('09:00:59');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!';

echo '<hr>';

$loginTime = strtotime('09:00:00');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!';


Check the already-asked question - how to get time difference in minutes:

Subtract the past-most one from the future-most one and divide by 60.

Times are done in unix format so they're just a big number showing the number of seconds from January 1 1970 00:00:00 GMT

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download