3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Input data $startTimestamp = '2023-08-05 14:45:05'; $endTimestamp = '2023-08-09 13:00:14'; // The interval expressed using `DateTime` objects $begin = new DateTimeImmutable($startTimestamp); $end = new DateTimeImmutable($endTimestamp); // The beginning of the first day (midnight) $start = $begin->setTime(0, 0, 0); // What its name says $oneDay = new DateInterval('P1D'); $output = []; // Check each day of the interval [$start, $end) $period = new DatePeriod($start, $oneDay, $end); foreach ($period as $day) { $s = max($day, $begin); // day start (midnight, except for the first day) $e = min($end, $day->add($oneDay)); // day end (midnight, except for the last day) $l = $e->diff($s); // day length // Express the day length in minutes $output[$day->format('Y-m-d')] = 24 * 60 * $l->d + 60 * $l->h + $l->i + round($l->s / 60); } print_r($output);

preferences:
31.78 ms | 407 KiB | 5 Q