1

我是 CodeIgniter 和 PHP 的新手。
在我的数据库中,我将用户状态存储在 IN 时间和 Out Time 中。我想计算该时间意味着用户在特定日期登录的时间。用户可能登录多次或在特定日期注销。
如何计算时间?

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

4

2 回答 2

3

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');
于 2012-08-04T11:28:58.193 回答
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);
于 2012-08-04T11:28:50.053 回答