<p>You could use my <a href="https://github.com/carlcs/craft-autoexpire" rel="nofollow">Auto Expire</a> 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:</p>

<pre><code>{ dateTime|date_modify('tomorrow 5am')|date('U') }
</code></pre>

<p>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.</p>

CC BY-SA 3.0
2015-05-13T11:46:08.727Z
2015-05-13T11:46:08.727Z
carlcs
<p>You could try using the <code>date_modify</code> filter with a <a href="http://php.net/manual/en/datetime.formats.relative.php" rel="nofollow">relative DateTime format</a>:</p>

<pre><code>{% if slide.dateTime|date_modify('tomorrow 5am')|date('U') &lt; now|date('U') %}
</code></pre>

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

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

{% set count = 0 %}
{% for slide in slides %}
    {% if count &lt; 5 and slide.dateTime|date_modify('tomorrow 5am')|date('U') &lt; now|date('U') %}
        {{ slide.dateTime }}
        {% set count = count + 1 %}
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-04-21T22:37:06.170Z
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!
Christopher Healey
CC BY-SA 3.0
2015-04-22T06:07:36.273Z
Christopher, comparing DateTime values probably works best with Unix timestamps. Updated the date filter argument to `date('U')`. Thanks
carlcs
CC BY-SA 3.0
2015-04-23T13:28:06.877Z
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?
Christopher Healey
CC BY-SA 3.0
2015-04-23T13:30:55.877Z
It would loop that slide but the counter wouldn't be increased.
carlcs
CC BY-SA 3.0
2015-04-23T13:32:23.410Z
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
CC BY-SA 3.0
2015-04-23T13:36:52.910Z
Are you using a custom variable as a counter and not loop.index? Also everything that outputs that slide should go into the conditional.
carlcs
CC BY-SA 3.0
2015-04-23T14:15:11.650Z
Would this not set a ridiculous load though not limiting the output in the for loop? I'd imagine after a year or so there would be a TON of entries that would be within the "slides" variable but not fit the conditional. Trying to wrap my head around this. You have skype by chance to 'type' chat with me real quick to help me to better understand why your method is preferable over mine? My skype: healey.christopher would greatly appreciate your time if you don't mind sparing a few minutes.
Christopher Healey
CC BY-SA 3.0
2015-04-23T17:58:34.017Z
After further testing it looks like you are right christian. I must have been testing for something else that had the same issue and then forgot to do further testing for the date expire option. Will create a new thread to see if anybody knows how to prevent all the additional queries.
Christopher Healey
CC BY-SA 3.0
2015-04-21T22:24:42.037Z
2015-04-22T06:05:18.330Z
2015-04-22T06:05:18.330Z
carlcs
<p>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. </p>

<p>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.</p>

<p>What I have currently is this:</p>

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

<p>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.</p>

<p>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.</p>

<p>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.</p>

<p>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.</p>

<datetime><forloop><math>
Loop based on date (with math)
# Loop based on date (with math)

(Asked by: username_0 on 2015-04-21)

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.

**Tags:** datetime, forloop, math

---

## 2 answers

### Answer by username_1 on 2015-05-13. Score: 2 | Accepted answer

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.


---

### Answer by username_1 on 2015-04-21. Score: 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 %}

> **Comments:**
> **username_0**: 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!  
> **username_1**: Christopher, comparing DateTime values probably works best with Unix timestamps. Updated the date filter argument to `date('U')`. Thanks  
> **username_0**: 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?  
> **username_1**: It would loop that slide but the counter wouldn't be increased.  
> **username_1**: 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.  
> **username_1**: Are you using a custom variable as a counter and not loop.index? Also everything that outputs that slide should go into the conditional.  
> **username_0**: Would this not set a ridiculous load though not limiting the output in the for loop? I'd imagine after a year or so there would be a TON of entries that would be within the "slides" variable but not fit the conditional. Trying to wrap my head around this. You have skype by chance to 'type' chat with me real quick to help me to better understand why your method is preferable over mine? My skype: healey.christopher would greatly appreciate your time if you don't mind sparing a few minutes.  
> **username_0**: After further testing it looks like you are right christian. I must have been testing for something else that had the same issue and then forgot to do further testing for the date expire option. Will create a new thread to see if anybody knows how to prevent all the additional queries.  

---

