如何计算php中两个时间戳之间的分钟差异

时间:2017-05-16 06:09:50

标签: php timestamp unix-timestamp difference

是否有任何本机函数可以获取两个时间戳之间的差异?我想得到两个时间戳的分钟差异。

编辑:抱歉,但实际上我只想要两个Unix时间戳的区别。

$date = new DateTime();
$date1_timestamp = $date->getTimestamp();
sleep('120');
$date2_timestamp = $date->getTimestamp();

function get_unix_timestamp_minutes_difference($start, $end) {
    /*
        Some code for return the difference between two unix timestamps
    */
}

echo get_timestamp_minutes_difference($date1_timestamp, $date2_timestamp);

2 个答案:

答案 0 :(得分:1)

您可以使用Carbon class http://carbon.nesbot.com/docs/并使用他的差异API:http://carbon.nesbot.com/docs/#api-difference

答案 1 :(得分:0)

试试这个:

$datetime1 = strtotime("2017-05-16 10:00:00");
$datetime2 = strtotime("2017-05-16 10:45:00");
$interval  = abs($datetime2 - $datetime1);
$minutes   = round($interval / 60);
echo 'Diff. in minutes is: '.$minutes;