# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: shortcodes, opening-hours, local-business
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/opening-hours-shortcode/

## Code

```php
// 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( '&ndash;', ', ' ), 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Opening Hours Shortcode With "Open Now" Status" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
