We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Conditionally show 'Open' or 'Closed' based on hours vs. current time

How Do I?

tacuster's avatar
tacuster
10 posts
10 years ago
tacuster's avatar tacuster

I’m trying to conditionally display an ‘Open’ or ‘Closed’ status on my website using hours vs. current time. I can’t seem to get the code to work. Any suggestions?

{hours}

{if
'{hours:open format="%H%i"}' <= '{current_time format="%H%i"}' &&
'{hours:close format="%H%i"}' > '{current_time format="%H%i"}' &&
'{hours:day}' == '{current_time format="%l"}' && '{hours:open_closed}' != 'Closed'
}

<span class="label pull-right">OPEN</span>
{/if}

{if
'{current_time format="%H%i"}' <= '{hours:open format="%H%i"}' &&
'{current_time format="%H%i"}' > '{hours:close format="%H%i"}' &&
'{hours:day}' == '{current_time format="%l"}' && '{hours:open_closed}' != 'Closed'
}
<span class="label pull-right">CLOSED</span>
{/if}

{hours}
       
jay_turley's avatar
jay_turley
46 posts
10 years ago
jay_turley's avatar jay_turley

(Edit 1 : went ahead and actually tried all this, so the following does work)

Assuming that you have an Hours Channel as follows:

day_of_week: text input; lowercase name of the day (monday, tuesday, etc.) (don’t use “{day}” because that’s an EE expression) open: text input; HHmm in military time format close: text input; HHmm in military time format

Here’s how I’d do it:

{exp:channel:entries channel="hours" search:day_of_week="{current_time format="%l"}" limit="1"}
  {if
    {current_time format="%H%i"} >= {open} && 
    {current_time format="%H%i"} <= {close}
  }
    <span class="store-availability">OPEN</span>
  {if:else}
    <span class="store-availability">CLOSED</span>
  {/if}
{/exp:channel:entries}

Edit 2: There is no need for a open/closed signifier for the day itself. Just enter times that can never be true in the conditional. For example if closed Sunday:

day_of_week: sunday
open: 2399
close: 0001
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.