使用date函数。date ( string $format [, int $timestamp ] )返回将整数 timestamp 按照给定的格式字串而产生的字符串。如果没有给出时间戳则返回本地当前时间。

//date()格式化时间返回String类型。 date("Y-m-d H:i:s")
//根据当前时间加一周后
也可以这样 date("Y-m-d",strtotime("-1 day")) ;直接获得前一天时间
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) . '分钟'; }