/** * @param $beginDate 开始日期 * @param $endDate 结束日期 * @return string */ function timeCalculation($beginDate, $endDate) { $subTime = strtotime($endDate) - strtotime($beginDate); $day = $subTime > 86400 ? floor($subTime / 86400) : 0; $subTime -= $day * 86400; $hour = $subTime > 3600 ? floor($subTime / 3600) : 0; $subTime -= $hour * 3600; $minute = $subTime > 60 ? floor($subTime / 60) : 0; $subTime -= $minute * 60; $second = $subTime; $dayText = $day ? $day . '天' : ''; $hourText = $hour ? $hour . '小时' : ''; $minuteText = $minute ? $minute . '天' : ''; $date = $dayText . $hourText . $minuteText . $second . '秒'; return $date; }
调用:
$result = $this->timeCalculation('2020-08-06 11:00:00', '2020-08-10 12:10:01'); echo $result; // 4天1小时10天1秒