如何在php中获得两次时间差

时间:2014-11-09 13:57:47

标签: php time

我有两次。我希望在秒之间得到时间差,假设有两个时间time1 = 4:20和time2 = 20:10现在我希望它们之间的秒数有所不同。

我这里没有日期参数,请不要将帖子标记为重复:Getting time difference between two times in PHP

那里也有一天,所以我的情况不同

3 个答案:

答案 0 :(得分:1)

$time1 = strtotime('01:30:00');
$time2 = strtotime('02:20:00');

$time_def = ($time2-$time1)/60;

echo 'Minutes:'.$time_def;

答案 1 :(得分:0)

如果4:20中的4是分钟而20是秒:

function minutesAndSecondsToSeconds($minutesAndSeconds) {
    list($minutes, $seconds) = explode(':', $minutesAndSeconds);
    return $minutes * 60 + $seconds;
}

echo minutesAndSecondsToSeconds('20:10') - minutesAndSecondsToSeconds('4:20');

有几个小时:

function timeToSeconds($time) {
    list($hours, $minutes, $seconds) = explode(':', $time);
    return $hours * 3600 + $minutes * 60 + $seconds;
}

echo timeToSeconds('3:20:10') - timeToSeconds('1:20:00');

或者只是使用strtotime,它应该按照这里解释的那样工作:Getting time difference between two times in PHP

答案 2 :(得分:0)

此功能适用于MM:SSHH:MM:SS格式:

function TimeToSec($time) {
    $sec = 0;
    foreach (array_reverse(explode(':', $time)) as $k => $v) $sec += pow(60, $k) * $v;
    return $sec;
}

要计算差异,请使用:

echo TimeToSec('20:10') - TimeToSec('4:20');

demo