Bricks Builder: Show/Hide elements based on Business Opening Hours

By Grant Ambrose

Here is how you can Show and Hide elements on the Page based on whether or not your Business is currently OPEN or CLOSED.

The more robust solution

For a more thorough solution, I put together this video below where I go through coding this.

The video is below and you can grab all the code from the blog post, too!

Show/Hide Elements based on "Business Opening Hours" - PHP challenge | bricks, acf options page

And here is the code below.

Create Your WordPress Website (without a developer)

Your Advanced Custom Fields need to match mine

NOTE: You will need to create the Options Page in Advanced Custom Fields with EXACTLY the same names for each field as I have below and in the video above.


<?php 

function is_store_open(){
    $open = false; // the store is closed by default
    
    $acf_exceptions_repeater = get_field('c_exception_business_hours_repeater', 'option');
    // pre_dump($acf_exceptions_repeater, "green");
    
    // get the current time of Susan's business = Settings > General
    $wp_timezone_obj = wp_timezone(); // timezone object from settings > general
    // pre_dump($wp_timezone_obj);
    $wp_timezone_name = $wp_timezone_obj->getName(); // Australia/Sydney

    // pre_dump($wp_timezone_name, 'black');
    
    $local_datetime_obj = new DateTimeImmutable("now", new DateTimeZone($wp_timezone_name));
    // pre_dump($local_datetime_obj, "green");
    
    $local_date = $local_datetime_obj->format("d M"); // 19 Feb
    $local_time = $local_datetime_obj->format("Hi"); // 1320
    $local_day_of_the_week = $local_datetime_obj->format("D"); // Mon, Tue, Wed etc
    // pre_dump($local_date, "purple");
    // pre_dump($local_time, "red");
    // pre_dump($local_day_of_the_week, "black");
    
    // check if our store is closed today because of the day of the week.
    $acf_general_business_hours_repeater = get_field('c_general_business_hours_repeater', 'option');
    
    // FUNCTION OPTION 1 - CHECKING WITH BOOLEAN
    // $current_day_found = false;
    // foreach($acf_general_business_hours_repeater as $acf_general_business_hours_day_of_week){
    //     $acf_day_of_week = $acf_general_business_hours_day_of_week['c_general_business_hours_day_of_the_week'];
    //     if($acf_day_of_week == $local_day_of_the_week){
    //         $current_day_found = true;
    //         // echo 'Today has been found. The day is ' . $acf_day_of_week;
    //     }
    //     else{
    //         // echo 'day not found.';
    //     }
    // }
    // if($current_day_found == false){
    //     return $open;
    // }
    
    // OPTION 2 - using ARRAY check with in_array()
    $acf_days_of_week_array = array();
    foreach($acf_general_business_hours_repeater as $acf_general_business_hours_day_of_week){
        $acf_days_of_week_array[] = $acf_general_business_hours_day_of_week['c_general_business_hours_day_of_the_week'];
    }
    // pre_dump($acf_days_of_week_array, 'green');
    if( in_array($local_day_of_the_week, $acf_days_of_week_array) ){
        // echo 'Today is a normal trading day for this business.';
    }
    else{
        return $open; // $open = false by default returned
        // echo 'today we are normally closed';
    }
    
    
    // check if the current local timezone from wp date matches a date in ACF exception days
    $exception_day_found = false;
    foreach($acf_exceptions_repeater as $acf_exceptions_child){
        if($acf_exceptions_child['c_exception_business_hours_date'] == $local_date){
            if($exception_day_found !== true){
                $exception_day_found = true;
            }
            // handling when we are closed ALL-DAY due to an Exception day
            if($acf_exceptions_child['c_exception_business_hours_closed_allday'] == true){
                // echo 'we are closed all day today';
                return $open; // $open = false;
            }else{ // we are not closed all day i.e. we are closed half day etc
                // pre_dump($acf_exceptions_child['c_exception_business_hours_open'], "green");
                // pre_dump($acf_exceptions_child['c_exception_business_hours_close'], "red");
                if( $local_time >= $acf_exceptions_child['c_exception_business_hours_open'] && 
                    $local_time < $acf_exceptions_child['c_exception_business_hours_close']
                ){
                    // echo 'we are currently open during the current timeframe for today';
                    return $open = true;
                }
                else{
                    // echo 'we are currently CLOSED during the current timeframe for today';
                }
            }
            // echo 'date found';
        }
        else{
            // echo 'date not found';
        }
    }
    if($exception_day_found){
        // there were exception days that matched the current date
        // but the current time did not match one of the timeframes, thus we have closed today
        return $open = false;
    }
    
    // echo 'hello';
    // handle if our store is OPEN today but there are no exceptions
    // check if current time is within default trading hours for this day of the week.
    // pre_dump($acf_general_business_hours_repeater, "blue");
    foreach($acf_general_business_hours_repeater as $acf_general_business_hours_repeater_item){
        if($acf_general_business_hours_repeater_item['c_general_business_hours_day_of_the_week'] == $local_day_of_the_week){
            $opentime = $acf_general_business_hours_repeater_item['c_general_business_hours_opening_time'];
            $closingtime = $acf_general_business_hours_repeater_item['c_general_business_hours_closing_time'];
            if( $local_time >= $opentime && $local_time < $closingtime){
                // we are open on this day
                // echo 'we are open on this day. the day is currently ' . $acf_general_business_hours_repeater_item['c_general_business_hours_day_of_the_week'];
                return $open = true;
            }
            else{
                // echo 'we are currently closed. the day is currently ' . $acf_general_business_hours_repeater_item['c_general_business_hours_day_of_the_week'];
            }
        }
    }
}

// FUNCTIONS FOR TESTING
if(!is_admin()){
    is_store_open();
}

function pre_dump($variable, string $color = 'red'){
    echo "<pre style='color:" . $color . "'>";
    var_dump($variable);
    echo "</pre>";
}
Code language: PHP (php)

Hopefully that helps!

The Simple Code (no options page)

Below is the code you can copy and paste if you just need very simple functionality.

<?php 

// adapted code from here
// https://stackoverflow.com/questions/14904864/determine-if-business-is-open-closed-based-on-business-hours

function wpirate_business_hours() {
    
    // force business hours status for debugging content on frontend
    if ( isset( $_GET['businesshours'] ) ) {
        $status = $_GET['businesshours'];
        return $status;
    }
    
    $status = 'closed';
    $storeSchedule = [
        'Mon' => ['08:00 AM' => '05:00 PM'],
        'Tue' => ['08:00 AM' => '05:00 PM'],
        'Wed' => ['08:00 AM' => '05:00 PM'],
        'Thu' => ['5:00 AM' => '9:00 AM', '12:00 PM' => '12:15 PM'],
        'Fri' => ['11:00 AM' => '05:00 PM']
    ];

    // get current time, change the timezone to what you need
    // https://www.php.net/manual/en/timezones
    $timeObject = new DateTime('Australia/Sydney');
    $timestamp = $timeObject->getTimeStamp();
    $currentTime = $timeObject->setTimestamp($timestamp)->format('H:i A');
    // comment out the below 3 lines on live site, I just added them for debugging
    $currentDay = date('D', $timestamp);
    //echo 'the current day is ' .  $currentDay . '<br>';
    //echo 'the current time is ' .  $currentTime . '<br>';

    // loop through time ranges for current day
    foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {

        // create time objects from start/end times and format as string (24hr AM/PM)
        $startTime = DateTime::createFromFormat('h:i A', $startTime)->format('H:i A');
        $endTime = DateTime::createFromFormat('h:i A', $endTime)->format('H:i A');
        
        //echo 'the start time for today is ' . $startTime . '<br>';
        //echo 'the end time for today is ' . $endTime . '<br>';

        // check if current time is within the range
        if (($startTime < $currentTime) && ($currentTime < $endTime)) {
            $status = 'open';
            break;
        }
    }
    return $status;
}
Code language: PHP (php)

3 comments

  • Sweet! I look forward to it.

  • Is it possible to add additional code to this if there are certain days (like holidays) that I would want the closed messaging to display on my site that I could set up ahead of time? Say if I knew now that the store would be closed on days like Thanksgiving or Christmas, could I add that in now instead of having to update it when those closed days actually are happening?

    • A
      Grant Ambrose

      Great question! I’ll make a video and post it on my YouTube here https://www.youtube.com/@GrantAmbrose/videos going through how to do that. I think that’s a great addition to this blog post. I’ll also go through how to create an Options Page using the Advanced Custom Fields plugin and try have it so you can set the opening times for each day of the week, plus specific holidays. I just need to work the code out first

Leave your comment

Not sure where to start?

Get my FREE 30-day Course:

QUICK-WINS for Solopreneurs using WordPress to grow their Online Business

The aim of this course is to help Solopreneurs like YOU overcome some of the most common problems you will face when trying to create, manage and update your Business’ WordPress website.

A QUICK-WIN is something you can go and implement into your website TODAY and see improvements instantly.

Each lesson is aimed at helping you overcome 1 headache associated with managing your WordPress website.

The perfect course for Solopreneurs building an Online Business with WordPress

Get Access Now
Some of my best tips and tricks!