营业时间使用矩阵

11

我正在构建一个企业目录,并希望不仅发布商家营业时间列表,还要发布当前是否营业的信息。

在矩阵中,我有7行,其中第一行代表周日,第七行代表周六。所以我有两个问题:

  1. 这段代码已经尽可能简洁了吗?还有更好的方法吗?
  2. 判断企业是否正在营业的条件是否有缺陷?它现在似乎可以工作,但没有进行过很多测试。
{!-- 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


你能否展示一下矩阵字段实际包含哪些列吗?可以截个屏或者粘贴一下什么的吗? - adrienne
2个回答

8

我觉得这很好,唯一需要更改的是在矩阵左侧添加另一列,并称其为"星期几",并加上一个下拉菜单以允许客户选择日期。然后在您的代码中,可以摆脱所有这些条件语句,只需替换为{day_of_week}即可。


1

这个逻辑不应该起作用:

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

您需要检查开放时间和关闭时间是否都小于current_time,而不是检查current_time是否在两个值之间。如果企业处于营业状态,则close_time应该比current_time更晚,而不是更早。逻辑应该是这样的:
{if 
    '{open_time format="%H%i"}' <= '{current_time format="%H%i"}' && 
    '{close_time format="%H%i"}' > '{current_time format="%H%i"}'
} 

如果我们要挑剔的话,那么如果有人需要为一周中完全关闭的企业输入数据,该怎么办呢?如果是我,我会在“全天关闭”列中添加一个PT Switch字段,并将其默认设置为否。这只需要对您现有的逻辑进行小的调整:

{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}

然后在 {hours_of_operation} 循环中:

{if closed_all_day != 'y'}
    {open_time format="%g:%i%a"} - {close_time format="%g:%i%a"}<br/>
{else}
    Closed<br/>
{/if}

我还没有仔细考虑过,但是我发现一个问题,当一个企业在午夜之后仍然营业时,比如一家酒吧从上午11点到凌晨2点开门... - Wedodan
那么,一旦过了午夜,{open_time} 就会大于 {current_time}(例如 1100 vs 0030),因此条件语句将被评估为 false。如果企业的开放和关闭时间在不同的日期,则需要更复杂的逻辑,到这种程度,自定义插件可能是最简洁的解决方案。如果您选择这条路线,strtotime 绝对会派上用场。 - Dom Stubbs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接