3v4l.org

run code in 500+ PHP versions simultaneously
<?php $time_start = [ 0 => '22:00:00', 1 => '03:00:00', ]; $time_end = [ 0 => '02:00:00', 1 => '08:00:00', ]; $logs = [ 0 => '2019-07-09 22:00:00', 1 => '2019-07-10 02:00:00', 2 => '2019-07-10 03:00:00', 3 => '2019-07-10 08:00:00', ]; $time = []; $diff = 0; for ($j = 0; $j < count($logs); $j = $j + 2) { $attendance_start = date("Y-m-d", strtotime($logs[$j])); $attendance_end = date("Y-m-d", strtotime($logs[$j + 1])); for ($i = 0; $i < count($time_start); $i++) { $time_starts = date('Y-m-d H:i:s', strtotime("$attendance_start " . $time_start[$i] . "")); $time_ends = date('Y-m-d H:i:s', strtotime("$attendance_end " . $time_end[$i] . "")); if ($logs[$j] >= $time_starts && $logs[$j + 1] <= $time_ends) { array_push($time, $logs[$j], $logs[$j + 1]); $diff += (round((strtotime($logs[$j + 1]) - strtotime($logs[$j])) / 3600, 1)); break; } } } echo $diff . " Hours";die;
Output for 7.1.25 - 7.1.30, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
9 Hours

preferences:
122.09 ms | 1424 KiB | 4 Q