Organizational Research By

Surprising Reserch Topic

getting time difference between two times in php


getting time difference between two times in php  using -'php,mysql,time'


  Possible Duplicate:
  How to get time difference in minutes in PHP  




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

i.e., if employe logged on or before 09:00:59 then its right time and the time diff should show be 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.

Thankyou in Advance.
    

asked Sep 14, 2015 by FernandoChan
0 votes
9 views



Related Hot Questions

3 Answers

0 votes

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

Demo

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

answered Sep 14, 2015 by BraHamill
0 votes

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)
answered Sep 14, 2015 by Moh23C
0 votes

answered Sep 14, 2015 by MakShowalte

...