
strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。
time 规定要解析的时间字符串。
now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。
成功则返回时间戳,否则返回 FALSE。在 PHP 5.1.0 之前本函数在失败时返回 -1。
思路是先求两个时间的秒数差,然后将结果转换即可:
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) . '分钟'; }