0

I'm attempting to create something within my template using global values and checkboxes. What I need this to do is "if it's one of the days of the week marked as open, as selected by a checkbox, and the time is between X and Y, then display that we are open, otherwise, say we are closed".

I have Monday through Sunday listed as checkboxes, and their values are set with Monday as 1, Tuesday as 2, etc, and I'm trying to compare it to 'now'|date('N'), which lists the day of the week as a number.

{% for dayOfWeek in theCenter.openWeekDay %}

    {% if 'now'|date('N') == dayOfWeek and 'now'|date('Hi', 'America/Denver') > theCenter.openTime|date('Hi') and 'now'|date('Hi', 'America/Denver') < theCenter.closeTime|date('Hi') %}
        <h3>We are OPEN</h3>
    {% else %}
        <h3>We are CLOSED</h3>
    {% endif %}

{% endfor %}

I've selected Tuesday through Sunday as open by selecting their checkboxes in the globals form. The code above returns 5 'We are Closed' and 1 'We are Open' results. I just need it to check if today is one of the open days, and if it's between open and closing time, and display one result.

I've tried this:

{% if 'now'|date('N') in dayOfWeek and 'now'|date('Hi', 'America/Denver') > theCenter.openTime|date('Hi') and 'now'|date('Hi', 'America/Denver') < theCenter.closeTime|date('Hi') %}

but it returns 6 'We are Closed'. I've been trying to figure this out for several hours, but the documentation doesn't seem to be made for what I want to do, and I'm probably looking in the wrong place.

Any suggestions on how I could get this to work?

EDIT

Here's the entire script:

{% if 'now'|date('z', 'America/Denver') > theCenter.startWinterSeason|date('z') or 'now'|date('z', 'America/Denver') < theCenter.endWinterSeason|date('z') %}
    {% for dayOfWeek in theCenter.openDaysWinter %}
        {% if dayOfWeek.selected and 'now'|date('N') == dayOfWeek and 'now'|date('Hi', 'America/Denver') > theCenter.openTimeWinter|date('Hi') and 'now'|date('Hi', 'America/Denver') < theCenter.closeTimeWinter|date('Hi') %}
            <h3>The center is OPEN</h3>
        {% else %}
            <h3>The center is CLOSED</h3>
        {% endif %}
    {% endfor %}
    <div class="infoPane">
        <p><i class="uk-icon-clock-o"></i>&nbsp;{{ theCenter.hoursTitleWinter }}</p>
        <p>Tuesday &mdash; Saturday<br>{{ theCenter.openTimeWinter|date('g:i A') }} to {{ theCenter.closeTimeWinter|date('g:i A') }}</p>
        <p class="uk-text-small">{{ theCenter.timeNotes|raw }}</p>
    </div>

{% elseif 'now'|date('z', 'America/Denver') > theCenter.startSummerSeason|date('z') and 'now'|date('z', 'America/Denver') < theCenter.endSummerSeason|date('z') %}
    {% for dayOfWeek in theCenter.openDaysSummer %}
        {% if dayOfWeek.selected and 'now'|date('N') == dayOfWeek and 'now'|date('Hi', 'America/Denver') > theCenter.openTimeSummer|date('Hi') and 'now'|date('Hi', 'America/Denver') < theCenter.closeTimeSummer|date('Hi') %}
            <h3>The center is OPEN</h3>
        {% else %}
            <h3>The center is CLOSED</h3>
        {% endif %}
    {% endfor %}
    <div class="infoPane">
        <p><i class="uk-icon-clock-o"></i>&nbsp;{{ theCenter.hoursTitleSummer }}</p>
        <p>Tuesday &mdash; Saturday<br>{{ theCenter.openTimeSummer|date('g:i A') }} to {{ theCenter.closeTimeSummer|date('g:i A') }}</p>
        <p class="uk-text-small">{{ theCenter.timeNotes|raw }}</p>
    </div>
{% else %}
    <h3>EPIC FAIL</h3>
{% endif %}

Which outputs this:

Output

I need it to only show the center is closed once.

  • I just setup a test case using virtually identical code and it worked as expected for me. Is your Craft install set to America/Denver under Settings->General? Also, have you seen github.com/pixelandtonic/StoreHours? – Brad Bell Jan 3 '16 at 21:27
  • I'll test out Store Hours and see if that works. Is there more documentation available on it? Also, the timezone is indeed, set to Denver. – Dustin C Jan 3 '16 at 21:42
  • I'm looking at this, and I realise that the reason it's spitting out "We are Closed" so many times is because it's stuck a loop. The hours are simple enough that we just want the hours listed on the site as "The center is open. Hours: Tuesday-Sunday, 9:00 AM to 4:30 PM" -- we don't want to list the hours out for each day of the week individually. – Dustin C Jan 3 '16 at 21:44
  • Hey @BradBell -- thanks for the Store Hours suggestion, but it's not giving me the level of control that I would need. I'll need to stick with this one for now. – Dustin C Jan 3 '16 at 21:52
  • I'm not following... the hours aren't being output at all, it's just looping on the days. Is your openTime and closeTime date/time fields set to just display the times? – Brad Bell Jan 3 '16 at 22:46
2

This should do what you're looking for:

{% set closed = true %}

{% for dayOfWeek in theCenter.openWeekDay %}

    {% if 'now'|date('N') == dayOfWeek and 'now'|date('Hi', 'America/Denver') > theCenter.openTime|date('Hi') and 'now'|date('Hi', 'America/Denver') < theCenter.closeTime|date('Hi') %}
        {% set closed = false %}
    {% endif %}

{% endfor %}

{% if closed %}
    <h3>The center is CLOSED</h3>
{% else %}
    <h3>The center is OPEN</h3>
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.