 
					凤凰求蛊
					这是一个可以完成您想要的 99% 的函数(它不实现四舍五入 when length != 'l',也不会删除sfrom eg 1 years)。0 值不会输出,尽管您可以添加一个参数来控制它。function friendlyDtmDiff($date1, $date2, $length = '', $format = '') {    // Create DateTime for diff()    $dt1 = new \DateTime($date1);    $dt2 = new \DateTime($date2);    // Create intervals    if ($dt1 < $dt2) {        $sign = '';        $interval = $dt1->diff($dt2);    }    else {        $sign = '-';        $interval = $dt2->diff($dt1);    }    // Output format (minimum 2 digits for upper case formats)    $of = ($format < 'a') ? '%02d' : '%d';    // generate output using an array of terms to be imploded    $output = array();    // create time components    switch ($format) {        case 'Y':        case 'y':            $years = $interval->y;            if ($years) $output[] = sprintf("$of years", $years);            if ($length != 'l') break;            $interval->y = 0;        case 'M':        case 'm':            $months = $interval->y * 12 + $interval->m;            if ($months) $output[] = sprintf("$of months", $months);            if ($length != 'l') break;            $interval->m = $interval->y = 0;        case 'D':        case 'd':            $days = ($interval->y * 12 + $interval->m) * 30 + $interval->d;            if ($days) $output[] = sprintf("$of days", $days);            if ($length != 'l') break;            $interval->d = $interval->m = $interval->y = 0;        case 'H':        case 'h':            $hours = (($interval->y * 12 + $interval->m) * 30 + $interval->d) * 24 + $interval->h;            if ($hours) $output[] = sprintf("$of hours", $hours);            if ($length != 'l') break;            $interval->h = $interval->d = $interval->m = $interval->y = 0;        case 'I':        case 'i':            $minutes = ((($interval->y * 12 + $interval->m) * 30 + $interval->d) * 24 + $interval->h) * 60 + $interval->i;            if ($minutes) $output[] = sprintf("$of minutes", $minutes);            if ($length != 'l') break;            $interval->i = $interval->h = $interval->d = $interval->m = $interval->y = 0;        case 'S':        case 's':            $seconds = (((($interval->y * 12 + $interval->m) * 30 + $interval->d) * 24 + $interval->h) * 60 + $interval->i) * 60 + $interval->s;            if ($seconds) $output[] = sprintf("$of seconds", $seconds);            break;        default:            return 'Invalid format';            break;    }    // put the output string together    $last = array_pop($output);    return $sign . (count($output) ? implode(', ', $output) . ' and ' : '') . $last;}用法示例:echo friendlyDtmDiff('2020-02-28 00:00:00', '2020-03-01 12:00:56', '', 'h') . PHP_EOL;echo friendlyDtmDiff('2020-02-28 00:00:00', '2020-03-01 12:00:56', 'l', 'h') . PHP_EOL;echo friendlyDtmDiff('2020-02-28 00:00:00', '2020-03-01 12:08:56', 'l', 'h') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 'y') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 'm') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 'd') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 'h') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 'i') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 's') . PHP_EOL;输出:60 hours60 hours and 56 seconds60 hours, 8 minutes and 56 seconds1 years, 3 months, 14 days, 4 hours, 8 minutes and 56 seconds15 months, 14 days, 4 hours, 8 minutes and 56 seconds464 days, 4 hours, 8 minutes and 56 seconds11140 hours, 8 minutes and 56 seconds668408 minutes and 56 seconds40104536 seconds3v4l.org 上的演示