繁体   English   中英

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

[英]How to calculate minutes difference between two timestamps in php

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

编辑:对不起,但实际上我只想要两个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);

您可以使用Carbon类http://carbon.nesbot.com/docs/并使用其不同的API: http : //carbon.nesbot.com/docs/#api-difference

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM