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 09:00:59
, then its right time and the time diff should be shown as null. If he logs in after the time i.e, 09:01:00
or later the time difference should be 00:00:01
. Like wise i need to calculate the differences in time.
One time is constant i.e., 09:00:59
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.
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
You can also use DateTime class:
$time1 = new DateTime('09:00:59'); $time2 = new DateTime('09:01:00'); $interval = $time1->diff($time2); echo $interval->format('%s second(s)');
Result:
1 second(s)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With