1

I would like to loop through entries only if the current date/time is before 5am on the following day from when the entry's eventDate (custom field mind you)is.

In other words I may have an event at 10pm on May/1/2015 and i want to loop through that date as long as it is not 5am (or later) on May/2/2015.

What I have currently is this:

{% for slide in craft.entries.section('events').featured('1').order('dateTime asc').limit(5).find() if (slide.dateTime|date("c","+8hours")) >= now|date("c") %}

However as you might have guessed, as far as i understand it, this loops through all entries as long as the current date/time is less than 8 hours AFTER the date/time of the event entry.

So to my understanding (Based on what i have observed) this will loop the entry up until 6am May/2/2015 which is not what i want.

In short. I'd like to ignore the 'time' portion of my date/time field as if it were May 1, 2015 12:00AM and add 29 hours, rather than including the time parameter of my date/time field and doing the math based on that which outputs variable results based on the time of the event.

Please let me know if i need to clarify any, I hope i've been clear. Also please let me know if there is anything else wrong with my For loop, it seems to work exactly how it should minus the date issue, but i'm always open to improvement.

2 Answers 2

2

You could use my Auto Expire plugin to make those entries expire the next morning 5 AM after the event. Just add a new expiration rule with the expiration date set to:

{ dateTime|date_modify('tomorrow 5am')|date('U') }

and you can now remove the logic from the templates. A nice side-effect is that expired entries are indicated with red traffic lights in the CP index view.

3

You could try using the date_modify filter with a relative DateTime format:

{% if slide.dateTime|date_modify('tomorrow 5am')|date('U') < now|date('U') %}

And if you want to limit the entries to be displayed, you should not use the limit parameter for that. Your ElementCriteriaModel doesn't take your DateTime conditional into account, so it requires a custom solution:

{% set slides = craft.entries.section('events').featured('1').order('dateTime asc').limit(null) %}

{% set count = 0 %}
{% for slide in slides %}
    {% if count < 5 and slide.dateTime|date_modify('tomorrow 5am')|date('U') < now|date('U') %}
        {{ slide.dateTime }}
        {% set count = count + 1 %}
    {% endif %}
{% endfor %}
8
  • Using your suggestion i changed it to be: {% for slide in craft.entries.section('events').featured('1').order('dateTime asc').limit(5).find() if (slide.dateTime|date_modify("tomorrow 5am")|date("c")) >= now|date("c") %} and appears to work perfectly. You are a god among men! Thanks! Apr 21, 2015 at 22:37
  • Christopher, comparing DateTime values probably works best with Unix timestamps. Updated the date filter argument to date('U'). Thanks
    – carlcs
    Apr 22, 2015 at 6:07
  • Carlcs my conditional seems to work as expected. In fact if i put the conditional as a child of the for loop rather than as a part of it, it seemed to still loop through items that did not meet the condition (it would not output them, but it'd still loop them) giving an incorrect # of items. Am i missing something? Apr 23, 2015 at 13:28
  • It would loop that slide but the counter wouldn't be increased.
    – carlcs
    Apr 23, 2015 at 13:30
  • It shouldn't matter where you put the conditional, I only removed it from the for tag because I thought it's more readable. To make it even more clear you could also split the two conditionals into separate tags.
    – carlcs
    Apr 23, 2015 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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