Answers.Body: <p>I think a better way would be to add a Preparse field to your Course Dates’ field layout that stores only the month.</p>
  
  <p><a href="https://github.com/aelvan/Preparse-Field-Craft" rel="nofollow noreferrer">https://github.com/aelvan/Preparse-Field-Craft</a></p>
  
  <p>The code for the <code>preparsedMonth</code> field.</p>
  
  <pre><code>{{ entry.courseStartDate|date('m') }}
  </code></pre>
  
  <p>With that in place you can drastically simplify your template code and reduce database queries.</p>
  
  <pre><code>{% set categoryParam = craft.request.getParam('month') %}
  
  {% set allDates = craft.entries({
      section: 'courseDates',
      courseStartDate: '&gt;= ' ~ now|date('c'),
      preparsedMonth: categoryParam ? '%02d'|format(categoryParam) : null,
      order: 'courseStartDate',
      limit: null,
  }) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-05T10:10:17.493Z
Answers.LastActivityDate: 2017-07-05T12:18:09.107Z
Answers.LastEditDate: 2017-07-05T12:18:09.107Z
Answers.UserDisplayName: carlcs
Body: <p>I'm building a calendar functionality and I need to get entries by month, but they cannot restricted by year (as, for example, the calendar might have entries that fall on both Jan 2017 and Jan 2018).</p>
  
  <p>At the moment, I'm doing a for loop and incrementing to get the next 5 years:</p>
  
  <pre><code>{% set categoryParam = craft.request.getParam('month') %}
  
  {% if categoryParam %}
    {% set allDates = [] %}
  
    {% for i in 0..5 %}
      {% set year = now|date('Y') + loop.index0 %}
      {% set combinedDate = '1970-' ~ categoryParam ~ '-01' %}
  
      {% set startDate = year ~ '-' ~ combinedDate|date('m') ~ '-01' %}
      {% set dayCount = startDate|date('t') %}
      {% set endDate  = year ~ '-' ~ combinedDate|date('m') ~ '-' ~ dayCount %}
  
      {% set courseDates = craft.entries({ section: 'courseDates', limit: null }).order('courseStartDate').courseStartDate('and','&gt;= ' ~ startDate, '&lt;=' ~ endDate) %}
  
      {% for courseDate in courseDates %}
        {% if courseDate not in allDates %}
          {% set allDates = allDates|merge([courseDate]) %}
        {% endif %}
      {% endfor %}
    {% endfor %}
  
  {% else %}
    {% set allDates = craft.entries({ section: 'courseDates', limit: null }).order('courseStartDate').courseStartDate('and','&gt;= ' ~ now) %}
  {% endif %}
  </code></pre>
  
  <hr>
  
  <p>I'm just wondering if there is a better way of doing this that doesn't rely on the year?</p>
  
Tags: <entries>
Title: Get entries by month, but not restricted by year
ThreadText: # Get entries by month, but not restricted by year
  
  (Asked by: username_0 on 2017-07-05)
  
  I'm building a calendar functionality and I need to get entries by month, but they cannot restricted by year (as, for example, the calendar might have entries that fall on both Jan 2017 and Jan 2018).
  
  At the moment, I'm doing a for loop and incrementing to get the next 5 years:
  
  ```
  {% set categoryParam = craft.request.getParam('month') %}
  
  {% if categoryParam %}
    {% set allDates = [] %}
  
    {% for i in 0..5 %}
      {% set year = now|date('Y') + loop.index0 %}
      {% set combinedDate = '1970-' ~ categoryParam ~ '-01' %}
  
      {% set startDate = year ~ '-' ~ combinedDate|date('m') ~ '-01' %}
      {% set dayCount = startDate|date('t') %}
      {% set endDate  = year ~ '-' ~ combinedDate|date('m') ~ '-' ~ dayCount %}
  
      {% set courseDates = craft.entries({ section: 'courseDates', limit: null }).order('courseStartDate').courseStartDate('and','>= ' ~ startDate, '<=' ~ endDate) %}
  
      {% for courseDate in courseDates %}
        {% if courseDate not in allDates %}
          {% set allDates = allDates|merge([courseDate]) %}
        {% endif %}
      {% endfor %}
    {% endfor %}
  
  {% else %}
    {% set allDates = craft.entries({ section: 'courseDates', limit: null }).order('courseStartDate').courseStartDate('and','>= ' ~ now) %}
  {% endif %}
  
  ```
  
  ---
  
  I'm just wondering if there is a better way of doing this that doesn't rely on the year?
  
  **Tags:** entries
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-07-05. Score: 3 | Accepted answer
  
  I think a better way would be to add a Preparse field to your Course Dates’ field layout that stores only the month.
  
  <https://github.com/aelvan/Preparse-Field-Craft>
  
  The code for the `preparsedMonth` field.
  
  ```
  {{ entry.courseStartDate|date('m') }}
  
  ```
  
  With that in place you can drastically simplify your template code and reduce database queries.
  
  ```
  {% set categoryParam = craft.request.getParam('month') %}
  
  {% set allDates = craft.entries({
      section: 'courseDates',
      courseStartDate: '>= ' ~ now|date('c'),
      preparsedMonth: categoryParam ? '%02d'|format(categoryParam) : null,
      order: 'courseStartDate',
      limit: null,
  }) %}
  
  ```
  
  
  ---
  
  