Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-13T11:46:08.727Z
Answers.LastActivityDate: 2015-05-13T11:46:08.727Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-21T22:37:06.170Z
Answers.Text: 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!
Answers.UserDisplayName: Christopher Healey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-22T06:07:36.273Z
Answers.Text: Christopher, comparing DateTime values probably works best with Unix timestamps. Updated the date filter argument to `date('U')`. Thanks
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-23T13:28:06.877Z
Answers.Text: 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?
Answers.UserDisplayName: Christopher Healey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-23T13:30:55.877Z
Answers.Text: It would loop that slide but the counter wouldn't be increased.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-23T13:32:23.410Z
Answers.Text: 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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-23T13:36:52.910Z
Answers.Text: Are you using a custom variable as a counter and not loop.index? Also everything that outputs that slide should go into the conditional.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-23T14:15:11.650Z
Answers.Text: 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.
Answers.UserDisplayName: Christopher Healey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-23T17:58:34.017Z
Answers.Text: 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.
Answers.UserDisplayName: Christopher Healey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-21T22:24:42.037Z
Answers.LastActivityDate: 2015-04-22T06:05:18.330Z
Answers.LastEditDate: 2015-04-22T06:05:18.330Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <datetime><forloop><math>
Title: Loop based on date (with math)
ThreadText: # 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](https://github.com/username_1/craft-autoexpire) 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](http://php.net/manual/en/datetime.formats.relative.php):
  
  ```
  {% 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**: username_0, comparing DateTime values probably works best with Unix timestamps. Updated the date filter argument to `date('U')`. Thanks  
  > **username_0**: username_1 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: username_0.username_0 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.  
  
  ---
  
  