php-计算时间差

计算两个时间的分钟差:
$timeTemp = time() – strtotime($sceneModel->start_time);
$diffMin = intval($timeTemp / 60);

计算年月日差距方法:

public static function getDateDiff($date1, $date2)
{
    //计算两个日期之间的时间差
    $diff = abs($date2 - $date1);

    //转换时间差的格式

    $years = floor($diff / (365 * 60 * 60 * 24));

    $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));

    $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

    $hours = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) /
        (60 * 60));

    $minutes = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 -
            $hours * 60 * 60) / 60);

    $seconds = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 -
        $hours * 60 * 60 - $minutes * 60));

    //printf("相差:%d 年, %d 月, %d 日, %d 小时, %d 分, %d 秒", $years, $months, $days, $hours, $minutes, $seconds);
    return $hours . '-' . $minutes . '-' . $seconds . ' ' . $hours . ':' . $minutes . ':' . $seconds;
}