【问题标题】:How to calculate minutes difference between two timestamps in php如何计算php中两个时间戳之间的分钟差
【发布时间】:2017-05-16 06:09:50
【问题描述】:

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

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

【问题讨论】:

标签: php timestamp unix-timestamp difference


【解决方案1】:

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

【讨论】:

  • 我刚刚检查了您提到的链接,它真的很有帮助。谢谢。
【解决方案2】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多