获取PHP中两次之间的时差 [英] Getting time difference between two times in PHP

查看:27
本文介绍了获取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.

即,如果员工在 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.

提前致谢.

推荐答案

您可以使用 strtotime() 用于时间计算.下面是一个例子:

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!';

演示

检查已经问过的问题 - 如何获得以分钟为单位的时差:

从最近的减去最近的,然后除以 60.

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

时间是以 unix 格式完成的,所以它们只是一个显示从 1970 年 1 月 1 日 00:00:00 GMT 开始的秒数

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆