DomStapleton
DomStapleton

Reputation: 23

Determining shop opening times in PHP

I'd like to include some text on my website that states whether or not a shop is open based on its opening times. If the shop is open, it says when it's open until. If it's not open, it says when it's next open.

I already have the opening times stored in the following variable:

$opening_times = [
    'Monday' => ['09:00' => '17:00'],
    'Tuesday' => ['09:00' => '17:00'],
    'Wednesday' => ['09:00' => '12:00'],
    'Thursday' => ['09:00' => '17:00'],
    'Friday' => ['09:00' => '17:00'],
    'Saturday' => ['09:30' => '17:00']
];

The shop is closed on Sunday.

Please could someone guide me as to how I can do this? I've already looked at this example but I'm unsure how to handle showing the time the shop is open next and what to do when it's a Sunday.

I'm hoping to finish with something that displays the next time the shop's open, whether that's on the same day or not. For example, at 5.30pm on Saturday, I'd like the message to say that the shop's next open at 9am on Monday.

I had previously attempted this by storing the next open day and time with each day in the $opening_times variable but I was wondering if there was a more elegant solution.

Upvotes: 0

Views: 3045

Answers (1)

Christian
Christian

Reputation: 1577

Using the answer here: Determine If Business Is Open/Closed Based On Business Hours as a guide.

This takes into account opening later and not opening today at all.

UPDATE: Tells you when it is next open. (Untested because work servers use PHP 5.3 :()

    <?php
$storeSchedule = [
    'Sun' => [['12:00' => '01:00', '09:00' => '12:00']],
    'Mon' => [['09:00' => '12:00']],
    'Tue' => [['09:00' => '12:00']],
    'Wed' => [['09:00' => '12:00']],
    'Thu' => [['09:00' => '12:00'], ['22:50' => '23:00']],
    'Fri' => [['09:00' => '12:00']],
    'Sat' => [['12:00' => '01:00', '09:00' => '12:00']]
];



// current or user supplied UNIX timestamp
$timestamp = time();

// default status
$open = false;

// Open later at
$openAt = false;

// get current time object
$currentTime = (new DateTime())->setTimestamp($timestamp);

// Current day
$currentDay = date('D', $timestamp);

if(isset($storeSchedule[$currentDay])){

    // loop through time ranges for current day
    foreach ($storeSchedule[$currentDay] as $key => $dateGroup) {
        $startTime = current(array_keys($dateGroup));
        $endTime = current(array_values($dateGroup));


        // create time objects from start/end times
        $startTime = DateTime::createFromFormat('H:i', $startTime);
        $endTime   = DateTime::createFromFormat('H:i', $endTime);

        // check if current time is within a range
        if (($startTime < $currentTime) && ($currentTime < $endTime)) {
            $open = true;
            break;
        }elseif($currentTime < $startTime){
            // Opening Later
            $openAt = $startTime;
        }
    }
}else{
    // Not open because day is not in array

}

if($open){
    echo "We are open";
}else{
    if($openAt){
        echo "We open later at " . $openAt->format('H:i');
    }else{
        // Get next open
        $arrayDays = array_keys($storeSchedule);            // Get an array of the days
        $arrayTimes = array_values($storeSchedule);         // Get an array of times
        $dayIndex = array_search($currentDay, $arrayDays);  // Find out what day we are in in the array. To see if there are more this week
        $nextDay = ($dayIndex + 1) >= count($arrayDays) ? $arrayTimes[0] : $arrayTimes[$dayIndex + 1]; // If there are no more this week, take the first day, else take the next day.
        $nextOpenTime   = current(array_keys($nextDay));    // Take the first set of times from this day as the start time
        $nextOpenDay    = $arrayDays[$dayIndex + 1];        // Get the day key name


        echo "We are not open";
        echo "We open next on " . $nextOpenDay . " at " . $nextOpenTime->format('H:i');
    }
}


?>

Upvotes: 4

Related Questions