Skip to Content
Menu

business_open()

Check if the requested datetime is within business hours.

PHP July 9, 2018

Usage

PHP
nebula()->business_open($date, $general)

Parameters

$date
(Optional) (String) The datetime to check
Default: Now

$general
(Optional) (Boolean) Check if the business is open that day
Default: false

Parameter Notes

General is a more vague check. Passing false is like asking “Is this business open at 3pm on November 24th?”, and passing true is like asking “Is this business open on November 24th?”.

Request or provide clarification »

Demo


Right now?

We are currently closed.


Tomorrow at any time?

We will be closed.


How about November 24th of this year?

We will be closed.

Additional Notes

This function has two convenient aliases: is_business_open() and is_business_closed().

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Functions.php on line 2009.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_business_open"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    PHP
            public function business_open($date=null, $general=false){
                $override = apply_filters('pre_business_open', null, $date, $general);
                if ( isset($override) ){return $override;}
    
                //Check object cache first so the full loop logic does not need to run more than once
                $is_business_open = wp_cache_get('is_business_open');
                if ( is_string($is_business_open) ){
                    if ( strtolower($is_business_open) === 'true' ){
                        return true;
                    }
    
                    return false;
                }
    
                nebula()->timer('Is Business Open');
    
                if ( $this->has_business_hours() ){
                    if ( empty($date) || $date === 'now' ){
                        $date = time();
                    } elseif ( strtotime($date) ){
                        $date = strtotime($date . ' ' . date('g:ia', strtotime('now')));
                    }
                    $today = strtolower(date('l', $date));
    
                    $businessHours = array();
                    foreach ( array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday') as $weekday ){
                        $businessHours[$weekday] = array(
                            'enabled' => $this->get_option('business_hours_' . $weekday . '_enabled'),
                            'open' => $this->get_option('business_hours_' . $weekday . '_open'),
                            'close' => $this->get_option('business_hours_' . $weekday . '_close')
                        );
                    }
    
                    $days_off = ( !empty($this->get_option('business_hours_closed')) )? $this->get_option('business_hours_closed') : ''; //Ensure correct type
                    $days_off = array_filter(explode(', ', $days_off));
                    if ( !empty($days_off) ){
                        foreach ( $days_off as $key => $day_off ){
                            $days_off[$key] = strtotime($day_off . ' ' . date('Y', $date));
    
                            if ( date('N', $days_off[$key]) === 6 ){ //If the date is a Saturday
                                $days_off[$key] = strtotime(date('F j, Y', $days_off[$key]) . ' -1 day');
                            } elseif ( date('N', $days_off[$key]) === 7 ){ //If the date is a Sunday
                                $days_off[$key] = strtotime(date('F j, Y', $days_off[$key]) . ' +1 day');
                            }
    
                            if ( date('Ymd', $days_off[$key]) === date('Ymd', $date) ){
                                nebula()->timer('Is Business Open', 'end');
                                wp_cache_set('is_business_open', 'false');
                                return false;
                            }
                        }
                    }
    
                    if ( $businessHours[$today]['enabled'] == '1' ){ //If the Nebula Options checkmark is checked for this day of the week.
                        if ( !empty($general) ){
                            nebula()->timer('Is Business Open', 'end');
                            wp_cache_set('is_business_open', 'true');
                            return true;
                        }
    
                        $openToday = date('Gi', strtotime($businessHours[$today]['open']));
                        $closeToday = date('Gi', strtotime($businessHours[$today]['close'])-1); //Subtract one second to ensure midnight represents the same day
                        if ( date('Gi', $date) >= $openToday && date('Gi', $date) <= $closeToday ){
                            nebula()->timer('Is Business Open', 'end');
                            wp_cache_set('is_business_open', 'true');
                            return true;
                        }
                    }
                }
    
                nebula()->timer('Is Business Open', 'end');
                wp_cache_set('is_business_open', 'false');
                return false;
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_business_open', 'my_custom_business_open', 10, 3); //The last integer must be 1 more than the actual parameters
    function my_custom_business_open($null, $date, $general){ //$null is required, but can be ignored
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_business_open', '__return_false');