3

I'm trying to show content based on the day and time of day. Specifically, the site I'm working on offers a live-stream every Sunday from 9:45 - 1:45 and we want to feature the live-stream on the homepage while it is live.

I have created a channel called "featured" which has an entry called Sunday. Using the following code, I can show the Sunday entry on Sunday.

To display the entry at a particular time, I created a Start Time and End Time field. Using that, I can display what I need to during the specified time.

The problem is that the entry has to be updated weekly to have the correct date. Is there a way to use start_time and end_time without a date? Or, every Sunday? Or, only Sunday?

    {exp:channel:entries channel="featured" url_title='{current_time format="%l"}'}
    {if current_time > start_time && current_time < end_time}
      <img src="{live-stream-image}">
    {if:else}
    <img src="{standard-image">
    {/if}{/exp:channel:entries}

As I said, this works but I'd like to not have to touch the entry each week to update the date. I'm not opposed to some other workflow, PHP, etc.

Thanks for any advice.

6
  • Are your start_time and end_time fields standard date field are are they text based? I'm wondering if you can set your conditional to check based on time: i.e., if your custom fields are date fields; {if "{current_time format='%h%i'}" >= "{start_time format='%h%i'}"} If your fields are text based you can probably match up the conditional by formatting current time into a comparable value. – pixldev Feb 15 '17 at 2:22
  • They are standard date fields. I'll give this a try. Thanks! – Brad Hopkins Feb 15 '17 at 11:38
  • 1
    Why do you need the fields? Why not just write a query in your template to 1-check its sunday, 2-check it matches the time slot, and if true display entry by entry ID? Yes it is hard coded and not search - but if the entry does not change, that should be no issue – Nevsie - Modeten Feb 17 '17 at 11:49
  • @Nevsie-Modeten that's a good suggestion. I didn't know how to write a query like that. What would it look like? – Brad Hopkins Feb 17 '17 at 14:52
  • Just exactly the same as your doing. Except instead of comparing two variables, your comparing it to a fixed value string or number. And then joining it with a && or AND. – Nevsie - Modeten Feb 18 '17 at 16:31
3

Thanks to @Nevsie-Modeten, I reworked the query to the following:

{if {current_time format='%w'} == 0 &&
    {current_time format='%G%i'} >= 930 &&
    {current_time format='%G%i'} <= 1230
}
    Content to be displayed during this time.
{if:else}
    Content to be displayed during other times.
{/if}

This checks the day and time and displays content appropriately. Using this method, I was able to eliminate the start_time and end_time channel fields that I created.

Thanks for taking the time to look and providing input!

1
  • Just a note that since you're using numeric comparisons, double-quoted strings are not necessary. Also you can use whitespace to make it more legible; I went ahead and edited your solution for those changes. :) – Derek Jones Mar 14 '17 at 14:58

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.