A nice addition to any business site is to display your opening hours. Use it anywhere you like, with your phone number, support form, or company address.
Basic code
In this example, I’m using a conditional with the {current_time}
queried against the current hour using the %H
date/time code variable for 24 hour clock time, so “17” is 5.00pm.
{if "{current_time format='%H'}" >= 09 AND "{current_time format='%H'}" < 17}
We're open for business!
{if:else}
We're closed right now
{/if}
What this does is display the open message if the time is greater than or equal to 9.00am and before 5.00pm (no later than 4.59pm to be precise), otherwise show the closed message.
But we don’t work weekends!
That’s no problem. We have the %N
date/time code that gives us day numbers to play with, where “1” is for Monday through to “7” for Sunday.
What we can do now is wrap our basic code with another conditional to check for the current day.
{if "{current_time format='%N'}" < 6}
{if "{current_time format='%H'}" >= 09 AND "{current_time format='%H'}" < 17}
We're open for business!
{if:else}
We're closed right now
{/if}
{if:else}
We don't work weekends, come back on Monday.
{/if}
So what we’re saying here, if the current day is less than 6 (i.e. before Saturday/Sunday) then output our daily open/closed message, otherwise say “we’re closed for the weekend”.
Caveats
By default
{current_time}
will always use your local server time so depending on where in the world your visitor is it may not reflect reality for them. If you have an international audience you can tweak your code further by catering for different time zones, see https://docs.expressionengine.com/latest/templates/date-variable-formatting.html for various time formats and parameters available.
- For the times to be dynamic you shouldn’t cache the template the code is in. I suggest you put the code in an uncached embed template, you can still cache the parent template to keep your page load time snappy.
Originally published by Rob Allen at u.expressionengine.com
Top comments (0)