Halloween Sale 15% off Ends in See the plans
PHP

Opening Hours Shortcode With "Open Now" Status

Adds an [opening_hours] shortcode that prints your weekly opening hours as a table, highlights today and shows whether you are open right now, based on the site time zone.

Don’t have WPIDE yet? Install it on your site

Code

// Day number (1 = Monday ... 7 = Sunday) => "HH:MM-HH:MM", several ranges separated by commas, or '' when closed.
$wpide_opening_hours = array(
    1 => '09:00-17:30',
    2 => '09:00-17:30',
    3 => '09:00-17:30',
    4 => '09:00-20:00',
    5 => '09:00-12:30,13:30-17:00',
    6 => '10:00-14:00',
    7 => '',
);

add_action( 'init', function () use ( $wpide_opening_hours ) {
    add_shortcode( 'opening_hours', function ( $atts ) use ( $wpide_opening_hours ) {
        $atts  = shortcode_atts( array( 'status' => 'yes' ), $atts, 'opening_hours' );
        $now   = new DateTimeImmutable( 'now', wp_timezone() );
        $today = (int) $now->format( 'N' );
        $time  = $now->format( 'H:i' );
        $open  = false;

        foreach ( array_filter( explode( ',', $wpide_opening_hours[ $today ] ?? '' ) ) as $range ) {
            list( $from, $to ) = array_map( 'trim', explode( '-', $range ) + array( '', '' ) );
            if ( $time >= $from && $time < $to ) {
                $open = true;
            }
        }

        $badge = sprintf(
            '<span class="wpide-open-status" style="display:inline-block;padding:.15em .6em;border-radius:1em;font-weight:600;color:#fff;background:%s">%s</span>',
            $open ? '#1a7f37' : '#b42318',
            $open ? 'Open now' : 'Closed now'
        );

        if ( 'only' === $atts['status'] ) {
            return $badge;
        }

        $html = 'yes' === $atts['status'] ? '<p>' . $badge . '</p>' : '';
        $html .= '<table class="wpide-opening-hours"><tbody>';
        foreach ( $wpide_opening_hours as $day => $hours ) {
            $name  = wp_date( 'l', strtotime( 'monday this week +' . ( $day - 1 ) . ' days' ) );
            $hours = '' === $hours ? 'Closed' : str_replace( array( '-', ',' ), array( '–', ', ' ), esc_html( $hours ) );
            $html .= sprintf( '<tr%s><th scope="row">%s</th><td>%s</td></tr>', $day === $today ? ' style="font-weight:700" aria-current="date"' : '', esc_html( $name ), $hours );
        }
        return $html . '</tbody></table>';
    } );
} );

Notes

Edit $wpide_opening_hours (use an empty string for closed days); add status="only" to print just the open or closed badge. Full-page caching can delay the status.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.