php 获取PHP中两次之间的时间差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13928021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:17:02  来源:igfitidea点击:

Getting time difference between two times in PHP

phpmysqltime

提问by Vignesh Gopalakrishnan

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:00or later the time difference should be 00:00:01. Like wise i need to calculate the differences in time.

即,如果员工登录或之前登录09:00:59,则其正确时间和时间差异应显示为空。如果他在时间 ie 之后登录,09:01:00或者更晚,则时差应该是00:00:01。同样明智,我需要计算时间差异。

One time is constant i.e., 09:00:59and 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.

先感谢您。

采纳答案by Sithu

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.

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

从最未来的减去过去最远的,然后除以 60。

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

回答by DroneZzZko

You can also use DateTimeclass:

您还可以使用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)

回答by zafus_coder

<?php
$start = strtotime("12:00");
$end = // Run query to get datetime value from db
$elapsed = $end - $start;
echo date("H:i", $elapsed);
?>