計算指定日期還有多少天?
思路是先求兩個時間的秒數差,然后將結果轉換即可:
echo calcTime('2018-08-20', '2018-08-30');function calcTime($fromTime, $toTime){ //轉時間戳 $fromTime = strtotime($fromTime); $toTime = strtotime($toTime); //計算時間差 $newTime = $toTime - $fromTime; return round($newTime / 86400) . '天' . round($newTime % 86400 / 3600) . '小時' . round($newTime % 86400 % 3600 / 60) . '分鐘'; }