ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Help with conditional statement based on current day of the week

July 26, 2011 6:42pm

Subscribe [2]
  • #1 / Jul 26, 2011 6:42pm

    aleklisefski

    24 posts

    Since there is no global variable (that can be used in a conditional statement) that outputs just the current day of the week, I created a snippet called current_day that includes:

    {current_time format="%l"}

    When testing the output of this snippet in a template as {current_day} it outputs correctly when placed inside OR outside of the EE channel entry loop. I’m seeing Monday, Tuesday, etc. based one what day it is.

    The trouble comes when I try to include {current_day} in a conditional statement. I am trying to show different content based on which day of the week it is. For example:

    {if current_day == "Monday"}
     // get monday content //
    {if:elseif current_day == "Tuesday"}
     // get tuesday content //
    {/if}

    When I do this, the condition is never met.

    If instead, just to test, I use:

    {if current_day}
     // get monday content //
    {/if}

    This returns true, so something is being passed through current_day, but clearly it’s not the day of the week as it is outputting on the same template by using {current_day}.

    I’ve tried every combination of using curly brackets, parenthesis and quotation marks just to make sure there was not some funny syntax that needed to be used with snippets. No luck with any of them.

    I’m stumped. This is the simplest conditional logic imaginable, and I know the snippet is working, so I have no clue how this doesn’t work.

    Ideas?

  • #2 / Jul 27, 2011 1:34pm

    Marcus Neto's avatar

    Marcus Neto

    1005 posts

    aleklisefski,

    First, welcome to the forums. Glad you are diving right in. Second, I think you are so close. 😊

    Let’s try this.

    {if (current_date format="l") == "Monday"}
    //Some Action
    {/if}

    That should get you where you wanted to be.

    Alternatively I love the Switchee add-on for this type of thing. It would make this a bit easier.

  • #3 / Jul 27, 2011 2:10pm

    aleklisefski

    24 posts

    Thanks Marcus,

    I should have been more thorough in my post. I already tried this as the first, most obvious solution. The reason I looked for other ways is that when I insert (current_date format=”%l”) into the conditional, it breaks my template and I get a blank, white screen (suppressed php error I assume?). So I assumed that was not an allowed use of {current_date}. Can you confirm that this does in fact work?

    I’m using the exact same code you posted above, and tried both “%l” and “l” as the date format. This does not work for me.

  • #4 / Jul 27, 2011 2:16pm

    aleklisefski

    24 posts

    Here’s a snipped of the actual code if it helps:

    {if (current_date format="%l") == "Monday"}
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th colspan="2">
                <a href="#%22class=%22button" class="button viewFullSchedule">View full schedule</a>
                <h3>{current_day}</h3>
    <p>        </th><br />
        </tr><br />
        {schedule-monday}<br />
        <tr class="{switch='none|odd'}"><br />
            <td width="33%"><strong>{time}</strong></td><br />
            <td width="66%">{class1}<br />
                {if class2}<br />
    {class2}{/if}<br />
                {if class3}<br />
    {class3}{/if}<br />
                {if class4}<br />
    {class4}{/if}<br />
                {if class5}<br />
    {class5}{/if}<br />
            </td><br />
        </tr><br />
        {/schedule-monday}<br />
    </table></p>
    
    <p>{if:elseif (current_date format="%l") == "Tuesday"}<br />
    <table cellpadding="0" cellspacing="0"><br />
        <tr><br />
            <th colspan="2"><br />
                <a href="#%22class=%22button" class="button viewFullSchedule">View full schedule</a><br />
                </p><h3>{current_day}</h3>
    <p>        </th><br />
        </tr><br />
        {schedule-tuesday}<br />
        <tr class="{switch='none|odd'}"><br />
            <td width="33%"><strong>{time}</strong></td><br />
            <td width="66%">{class1}<br />
                {if class2}<br />
    {class2}{/if}<br />
                {if class3}<br />
    {class3}{/if}<br />
                {if class4}<br />
    {class4}{/if}<br />
                {if class5}<br />
    {class5}{/if}<br />
            </td><br />
        </tr><br />
        {/schedule-tuesday}<br />
    </table><br />
    {/if}

  • #5 / Jul 27, 2011 2:40pm

    Marcus Neto's avatar

    Marcus Neto

    1005 posts

    You are correct. That version was broke 😊

    But I did check this version out and it works a charm.

    {if '{current_time format='%l'}' == "Tuesday"}
    Hip Hip
    {if:elseif '{current_time format='%l'}' == "Wednesday"}
    Hooray!
    {if:elseif '{current_time format='%l'}' == "Thursday"}
    Hoooo!
    {/if}
  • #6 / Jul 27, 2011 2:45pm

    kirkaracha

    273 posts

    Couldn’t you enable PHP on input and do this?

    <?php $current_date = date("l"); ?>
    
    <?php if($current_date == 'Monday'): ?>
    {schedule-monday}
    <?php elseif($current_date == 'Tuesday'): ?>
    {schedule-tuesday}
    <?php endif; ?>
  • #7 / Jul 27, 2011 2:57pm

    aleklisefski

    24 posts

    Marcus,

    Brilliant! Works perfectly! Thanks. Feel free to close this up.

  • #8 / Jul 27, 2011 3:32pm

    Marcus Neto's avatar

    Marcus Neto

    1005 posts

    Glad to hear it.

    Closing thread.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases