Possible Duplicate:
How to get time difference in minutes in PHP可能重复:如何在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.
即,如果员工登录或在09:00:59之前,则其正确的时间和时间差异应显示为空。如果他在时间之后登录,即09:01:00或更晚,时差应为00:00:01。同样明智的我需要及时计算差异。
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.
一次是不变的,即09:00:59,另一次是从数据库表中得到的。需要在两者之间获得差异。我在PHP工作。希望我的问题很清楚。
Thank you in Advance.
先谢谢你。
3 个解决方案
#1
24
You can use strtotime() for time calculation. Here is an example:
您可以使用strtotime()进行时间计算。这是一个例子:
$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!';
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.
从过去最多的一个减去过去最多的一个并除以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
时间以unix格式完成,因此它们只是一个大数字,显示从格林威治标准时间1970年1月1日00:00:00开始的秒数
#2
14
You can also use DateTime class:
您还可以使用DateTime类:
$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)
#3
-1
<?php
$start = strtotime("12:00");
$end = // Run query to get datetime value from db
$elapsed = $end - $start;
echo date("H:i", $elapsed);
?>
#1
24
You can use strtotime() for time calculation. Here is an example:
您可以使用strtotime()进行时间计算。这是一个例子:
$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!';
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.
从过去最多的一个减去过去最多的一个并除以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
时间以unix格式完成,因此它们只是一个大数字,显示从格林威治标准时间1970年1月1日00:00:00开始的秒数
#2
14
You can also use DateTime class:
您还可以使用DateTime类:
$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)
#3
-1
<?php
$start = strtotime("12:00");
$end = // Run query to get datetime value from db
$elapsed = $end - $start;
echo date("H:i", $elapsed);
?>