11

I am building a directory of businesses and want to not only publish a list of business hours, but also to publish if a business is currently open for business.

In a matrix, I have 7 rows with row_1 representing Sunday row_7 for Saturday. So I have two questions.

  1. Is this as concise as the code COULD be or is there a better way?
  2. Is there a flaw in the conditional that tells whether a business is currently open? It seems to work right now, but not very tested.

    {!-- Hours of Operation --}  
    {exp:stash:set name="hours-of-operation"}
    The Current time is: {current_time format="%g:%i%a"}<br/>
       {hours_of_operation}
       {if row_count=="1"}Sunday{/if}
       {if row_count=="2"}Monday{/if}
       {if row_count=="3"}Tuesday{/if}
       {if row_count=="4"}Wednesday{/if}
       {if row_count=="5"}Thursday{/if}
       {if row_count=="6"}Friday{/if}
       {if row_count=="7"}Saturday{/if}
       {open_time format="%g:%i%a"} - {close_time format="%g:%i%a"}<br/>
       {/hours_of_operation}
    {/exp:stash:set} 
    {!-- Hours of Operation --}
    
    {!-- Are we open? --}
    {exp:stash:set name="are-we-open"}
    {exp:mx_calc expression='{current_time format="%w"}+1'}
        {!-- matrix --}
        {hours_of_operation}                
            {if row_count=="{calc_result}"}
                Today is: {current_time format="%l"}<br/>
        <strong>
                {if '{open_time format="%H%i"}' <= '{current_time format="%H%i"}' && '{close_time format="%H%i"}' <= '{current_time format="%H%i"}'}    
                We are currently open!{if:else}We are currently closed.
            {/if}
            </strong><br/>
                Today's Hours are:<br/> <strong>{open_time format="%g:%i%a"} - {close_time format="%g:%i%a"}</strong><br/>              
            {/if}   
        {/hours_of_operation} 
        {!-- matrix --}
    {/exp:mx_calc}
    {/exp:stash:set}
    {!-- Are we open? --}
    

enter image description here

Wedodan
  • 371
  • 1
  • 6
  • Can you show us what columns are actually in the Matrix field? A screenshot or a paste or something? – adrienne Oct 30 '12 at 20:33

2 Answers2

8

This looks good to me, the only thing I would change is add another column on the left of the matrix and call it day of week with a drop down to allow the client to select the day. then in your code you can get rid of all those conditionals and replace it with just {day_of_week}

CreateSean
  • 1,286
  • 1
  • 21
  • 42
1

This logic shouldn't work:

{if '{open_time format="%H%i"}' <= '{current_time format="%H%i"}' && '{close_time format="%H%i"}' <= '{current_time format="%H%i"}'} 

You're checking that both the closing and opening times are less than current_time, rather than checking that current_time is between the two values. If the business is open then close_time should be more than current_time, not less. The logic should be:

{if 
    '{open_time format="%H%i"}' <= '{current_time format="%H%i"}' && 
    '{close_time format="%H%i"}' > '{current_time format="%H%i"}'
} 

Also if we're being picky, what do people do if they have to input data for a business that's closed entirely for one or more days of the week? If it were me I'd throw a PT Switch field in as a 'Closed All Day' column, defaulting to no. It would only require a small tweak to your existing logic:

{if
    '{open_time format="%H%i"}' <= '{current_time format="%H%i"}' && 
    '{close_time format="%H%i"}' > '{current_time format="%H%i"}' && 
    '{closed_all_day}' != 'y'
}    
    We're currently open!
{if:else}

Then in the {hours_of_operation} loop:

{if closed_all_day != 'y'}
    {open_time format="%g:%i%a"} - {close_time format="%g:%i%a"}<br/>
{else}
    Closed<br/>
{/if}
Dom Stubbs
  • 1,198
  • 7
  • 15
  • I haven't thought much about it, but I am seeing an issue with this when a business is open past midnight, lets say a bar is open from 11am to 2am... – Wedodan Nov 06 '12 at 18:55
  • That would follow, as once you're past midnight `{open_time}` is greater than `{current_time}` (e.g. 1100 vs 0030) so the conditional evaluates as false. If the business opening and closing times are on different days some more complex logic is required, to the extent that a custom plugin would probably be the neatest solution. If you go down that route, `strtotime` would definitely come in handy. – Dom Stubbs Nov 06 '12 at 20:38