3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Based on the following business hours: * (Note : I setup the hours for each day if they carry-over) * everyday is open from 09:00 AM - 12:00 AM * Sun/Sat open extra from 12:00 AM - 01:00 AM */ $storeSchedule = [ 'Sun' => ['3:00 PM' => '09:00 PM'], 'Tue' => ['09:00 AM' => '12:00 PM'], 'Wed' => ['09:00 AM' => '12:00 PM'], 'Thu' => ['09:00 AM' => '12:00 PM'], 'Fri' => ['09:00 AM' => '12:00 PM'], 'Sat' => ['12:00 AM' => '01:00 PM', '09:00 AM' => '12:00 AM'] ]; // current OR user supplied UNIX timestamp $timestamp = time(); // default status $status = 'closed'; // get current time object $currentTime = (new DateTime())->setTimestamp($timestamp); // loop through time ranges for current day foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) { // create time objects from start/end times $startTime = DateTime::createFromFormat('h:i A', $startTime); $endTime = DateTime::createFromFormat('h:i A', $endTime); // check if current time is within a range if (($startTime < $currentTime) && ($currentTime < $endTime)) { $status = 'open'; break; } } echo "We are currently: $status";

preferences:
31.48 ms | 402 KiB | 5 Q