如何在同一天两次之间获得时差?

时间:2012-08-04 11:23:51

标签: php model-view-controller codeigniter

我是CodeIgniter&的新手。 PHP。
在我的数据库中,我将用户状态存储在IN时间和Out Time中。我想计算一下这个时间是指用户在特定日期登录的时间。用户可能会登录多次或在特定日期退出 我该如何计算时间?

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

2 个答案:

答案 0 :(得分:3)

DateTime::diff()功能可以满足您的需求。

e.g。

$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');

答案 1 :(得分:2)

使用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);
相关问题