如何获取同一天中两个时间之间的时差



我是CodeIgniter&PHP
在我的数据库中,我将用户状态存储在In时间和Out时间中。我想计算一下时间的含义,即用户在某一天登录的时间。用户可能登录不止一次,或者在某一天注销
我如何计算时间?

时间格式为=04:06:37。如果用户登录04:06:37并注销04:09:37,则时间差为3分0秒。

DateTime::diff()函数可以执行您想要的操作。

例如

$login    = new DateTime('2012-09-04 14:00:00');
$logout   = new DateTime('2012-09-04 16:00:00');
$interval = $logout->diff($login);
echo $interval->format('%H hours %i minutes %s seconds');

使用strtotime()将时间转换为时间戳,然后从原始时间减去最近的时间。例如

$to_time = strtotime("04:09:37");
$from_time = strtotime("04:06:37");
$time_diff = $to_time - $from_time;
echo gmdate('H:i:s', $time_diff);

最新更新