3v4l.org

run code in 300+ PHP versions simultaneously
<?php $time_array = ["17:45", "13:12", "09:29", "17:32", "16:49", "14:18"]; $buckets = array_fill_keys([-1, 0, 1], 0); // initialize buckets with 0 values foreach ($time_array as $time) { ++$buckets[(int)sqrt(substr($time, 0, 2) - 8) <=> 2]; } echo "Morning Count: {$buckets[-1]}\n"; // Hours: 00:00 to 11:59 ->12hrs (calculates as: 0, 1) echo "Afternoon Count: {$buckets[0]}\n"; // Hours: 12:00 to 16:59 -> 5hrs (calculates as: 2) echo "Evening Count: {$buckets[1]}"; // Hours: 17:00 to 23:59 -> 7hrs (calculates as: 3, 4) echo "\n---\n"; foreach (range(0, 23) as $t) { $calc = (float)sqrt($t - 8); echo "$t: " , (int)$calc , " ... [float value from sqrt was: $calc]\n"; }

preferences:
111.93 ms | 407 KiB | 5 Q