Skip to Content

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 open!


How about November 24th of this year?

We will be open!

Additional Notes

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

Source File

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

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;}

        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 = array_filter(explode(', ', $this->get_option('business_hours_closed')));
        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) ){
                    return false;
                }
            }
        }

        if ( $businessHours[$today]['enabled'] == '1' ){ //If the Nebula Options checkmark is checked for this day of the week.
            if ( !empty($general) ){
                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 ){
                return true;
            }
        }

        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');