You can do this by subtracting a day from all values where the time is later than 10pm:
$times = ['23:00', '01:00']; $total = 0; foreach ($times as $time) { $dt = new DateTime($time); // Treat all values later than 10pm as the previous day if ($dt->format('H') >= 22) { $dt->modify('-1 day'); } // Sum the timestamps $total += $dt->format('U'); } $average = $total / count ($times); echo date('Y-m-d H:i:s', $average);
Question and answer is powered by AnsPress.io