3v4l.org

run code in 300+ PHP versions simultaneously
<?php function roundTime($timestamp, $interval, $direction){ $rounded_seconds = 0; switch ($direction) { case "u": //round up $rounded_seconds = ceil($timestamp / ($interval * 60)) * ($interval * 60); break; case "d": //round down $rounded_seconds = floor($timestamp / ($interval * 60)) * ($interval * 60); break; default: echo "default"; } return date('H:i', $rounded_seconds) . "\n"; } $testTimes = [ "2023-05-30 14:05", "2023-05-30 14:23", "2023-05-30 14:35", "2023-05-30 14:40", "2023-05-30 14:55" ]; foreach ($testTimes as $time) { echo "base time: ".$time.PHP_EOL; echo "round up to 15 mins: ".roundTime(strtotime($time), 15, "u"); echo "round up to 30 mins: ".roundTime(strtotime($time), 30, "u"); echo "round down to 15 mins: ".roundTime(strtotime($time), 15, "d"); echo "round down to 30 mins: ".roundTime(strtotime($time), 30, "d"); echo PHP_EOL; }
Output for 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
base time: 2023-05-30 14:05 round up to 15 mins: 14:15 round up to 30 mins: 14:30 round down to 15 mins: 14:00 round down to 30 mins: 14:00 base time: 2023-05-30 14:23 round up to 15 mins: 14:30 round up to 30 mins: 14:30 round down to 15 mins: 14:15 round down to 30 mins: 14:00 base time: 2023-05-30 14:35 round up to 15 mins: 14:45 round up to 30 mins: 15:00 round down to 15 mins: 14:30 round down to 30 mins: 14:30 base time: 2023-05-30 14:40 round up to 15 mins: 14:45 round up to 30 mins: 15:00 round down to 15 mins: 14:30 round down to 30 mins: 14:30 base time: 2023-05-30 14:55 round up to 15 mins: 15:00 round up to 30 mins: 15:00 round down to 15 mins: 14:45 round down to 30 mins: 14:30

preferences:
55.1 ms | 1062 KiB | 4 Q