3v4l.org

run code in 300+ PHP versions simultaneously
<?php 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;
Output for 7.1.25 - 7.1.31, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.4 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
60 hours 60 hours and 56 seconds 60 hours, 8 minutes and 56 seconds 1 years, 3 months, 14 days, 4 hours, 8 minutes and 56 seconds 15 months, 14 days, 4 hours, 8 minutes and 56 seconds 464 days, 4 hours, 8 minutes and 56 seconds 11140 hours, 8 minutes and 56 seconds 668408 minutes and 56 seconds 40104536 seconds

preferences:
81.09 ms | 408 KiB | 5 Q