在PHP中获得两次之间的时差 [英] Getting time difference between two times in PHP
问题描述
可能重复:
如何在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.
,即,如果员工在09:00:59
之前或之前登录,则其正确时间和时间差应显示为空.如果他在09:01:00
或更晚的时间后登录,则时差应为00:00:01
.明智的做法是,我需要计算时间差.
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
,另一个是我从数据库表中获取的时间.需要获取两者之间的差异.
我正在使用PHP.
希望我的问题清楚.
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.
先谢谢您
推荐答案
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!';
演示
检查已经提出的问题-如何在几分钟内获得时差:
Demo
Check the already-asked question - how to get time difference in minutes:
从将来的一中减去过去的一并除以60.
Subtract the past-most one from the future-most one and divide by 60.
时间以Unix格式完成,因此它们只是一个很大的数字,显示了 从格林尼治标准时间1970年1月1日00:00:00起的秒数
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
这篇关于在PHP中获得两次之间的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!