【问题标题】:Hours of Operation using Matrix使用 Matrix 的营业时间
【发布时间】:2012-10-20 06:29:42
【问题描述】:

我正在建立一个企业目录,不仅要发布营业时间列表,还要发布企业当前是否营业。

在一个矩阵中,我有 7 行,其中 row_1 代表周六的周日 row_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? --}
    

【问题讨论】:

  • 您能告诉我们矩阵字段中实际包含哪些列吗?截图或粘贴什么的?

标签: expressionengine


【解决方案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 点...
  • 这将随之而来,因为一旦您过了午夜,{open_time} 大于 {current_time}(例如 1100 与 0030),因此条件评估为假。如果业务开放和关闭时间在不同的日子,则需要一些更复杂的逻辑,以至于自定义插件可能是最简洁的解决方案。如果你走这条路,strtotime 肯定会派上用场。
【解决方案2】:

这对我来说看起来不错,我唯一要更改的是在矩阵左侧添加另一列并将其命名为星期几,并通过下拉菜单允许客户选择日期。然后在您的代码中,您可以摆脱所有这些条件并将其替换为 {day_of_week}

【讨论】:

    猜你喜欢
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2015-04-11
    • 2011-02-12
    • 1970-01-01
    • 2014-05-31
    相关资源
    最近更新 更多