• Resolved Generosus

    (@generosus)


    The code snippet provided below generates the shortcode “[business_hours]” which can be used to display a message on the front end indicating if a business is open or closed based on normal business hours.

    We would like to modify it to add holidays together with the front end message: “CLOSED FOR THE HOLIDAY”

    We do not need to use this plugin in its entirety.

    Any experts available to help?

    Thank you!

    PS: We tried to integrate portions of the code provided here, but couldn’t get them to work.

    ———————-

    function business_hours_shortcode($atts) {
    if (is_page ('contact')) {
    // Set timezone - change this to match your own.
    date_default_timezone_set('America/Chicago');

    // An array of your opening hours.
    $opening_hours = array(
    'Monday' => array('07:30', '17:30'),
    'Tuesday' => array('07:30', '17:30'),
    'Wednesday' => array('07:30', '17:30'),
    'Thursday' => array('07:30', '17:30'),
    'Friday' => array('07:30', '17:30'),
    'Saturday' => array('Closed'),
    'Sunday' => array('Closed'),
    );

    // Get current day and time.
    $current_day = date('l');
    $current_time = date('H:i');

    // Get today's opening hours.
    $today_hours = $opening_hours[$current_day];

    // Check if we have opening hours for today.
    if (!isset($today_hours) || count($today_hours) < 2) {
    return '<span style="color:#ffffff;background-color:#e14d43;padding:6px 12px;border-radius:3px;">CLOSED TODAY</span>';
    }

    // Check if current time is between opening hours.
    if ($current_time >= $today_hours[0] && $current_time <= $today_hours[1]) {
    return '<span style="color:#ffffff;background-color:#00c853;padding:6px 12px;border-radius:3px;">CURRENTLY OPEN</span>';
    } else {
    return '<span style="color:#ffffff;background-color:#e14d43;padding:6px 12px;border-radius:3px;">CURRENTLY CLOSED</span>';
    }
    }
    }
    add_shortcode('business_hours', 'business_hours_shortcode');
Viewing 1 replies (of 1 total)
  • Thread Starter Generosus

    (@generosus)

    Solved! Added new code (in bold).

    function business_hours_shortcode($atts) {
    if (is_page ('contact')) {
    // Set timezone - change this to match your own.
    date_default_timezone_set('America/Chicago');

    // An array of your opening hours.
    $opening_hours = array(
    'Monday' => array('07:30', '17:30'),
    'Tuesday' => array('07:30', '17:30'),
    'Wednesday' => array('07:30', '17:30'),
    'Thursday' => array('07:30', '17:30'),
    'Friday' => array('07:30', '17:30'),
    'Saturday' => array('Closed'),
    'Sunday' => array('Closed'),
    );

    // An array of your holidays (format: month/day - add zero before single-digit month or day)
    $holidays = array('01/01', '05/26', '07/04', '07/05', '09/02', '11/11', '1/28', '11/29', '12/24', '12/25', '12/31');


    // Get current date, day and time.
    $current_date = date('m/d');
    $current_day = date('l');
    $current_time = date('H:i');

    // Get today's opening hours.
    $today_hours = $opening_hours[$current_day];

    // Check if today is a holiday.
    if (in_array($current_date,$holidays)) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;opacity:.7;padding:6px 12px;border-radius:3px;">CLOSED FOR HOLIDAY</span>';
    }


    // Check if we have opening hours for today.
    if (!isset($today_hours) || count($today_hours) < 2) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;opacity:.7;padding:6px 12px;border-radius:3px;">CLOSED TODAY</span>';
    }

    // Check if current time is between opening hours.
    if ($current_time >= $today_hours[0] && $current_time <= $today_hours[1]) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#00c853;opacity:.7;padding:6px 12px;border-radius:3px;">CURRENTLY OPEN</span>';
    } else {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;opacity:.7;padding:6px 12px;border-radius:3px;">CURRENTLY CLOSED</span>';
    }
    }
    }
    add_shortcode('business_hours', 'business_hours_shortcode');
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.