<p>Just an update to show final current version of code for calendar - re stefan comment.</p>

<pre><code>  {% if year is defined and month is defined %}
      {% set time = (year ~ "-" ~ month ~ "-" ~ "01") %}
      {% set catslug = craft.app.request.segment(2) %}
      {% if catslug != 'all' %}
        {% set category = craft.categories.group('eventFilters').slug(catslug) %}
      {% endif %}
  {% endif %}
  {% if time is not defined%} 
      {% set time = "now" %}
  {% endif %}
  {% if catslug is not defined or catslug is empty%} 
      {% set catslug = "all" %}
  {% endif %}

  {% set thisyear = time|date("Y") %}
  {% set thismonth = time|date("m") %}
  {% set prevyear = time|date_modify("-1 month")|date("Y") %}
  {% set prevmonth = time|date_modify("-1 month")|date("m") %}
  {% set nextyear = time|date_modify("+1 month")|date("Y") %}
  {% set nextmonth = time|date_modify("+1 month")|date("m") %}



  &lt;!-- CONTENT --&gt;

  &lt;div class="row calhead text-center align-items-center py-2 mb-3"&gt;
      &lt;div class="col-auto"&gt;
        &lt;a class="calchange calleft" href="#" data-slug="{{ catslug }}" data-month="{{ prevmonth }}" data-year="{{ prevyear }}"&gt;
          &lt;span class="fa-stack "&gt;
            &lt;i class="fas fa-circle fa-stack-2x"&gt;&lt;/i&gt;
            &lt;i class="fas fa-angle-left fa-stack-1x fa-inverse"&gt;&lt;/i&gt;
          &lt;/span&gt;  
        &lt;/a&gt;
      &lt;/div&gt;

      &lt;div class="col"&gt;&lt;div class="caldate"&gt;{{ time|date("F")}} {{ time|date("Y")}}&lt;/div&gt;&lt;/div&gt;
      &lt;div class="col-auto"&gt;
        &lt;a class="calchange calright" href="#" data-slug="{{ catslug }}" data-month="{{ nextmonth }}" data-year="{{ nextyear }}"&gt;
          &lt;span class="fa-stack "&gt;
            &lt;i class="fas fa-circle fa-stack-2x"&gt;&lt;/i&gt;
            &lt;i class="fas fa-angle-right fa-stack-1x fa-inverse"&gt;&lt;/i&gt;
          &lt;/span&gt;          
        &lt;/a&gt;
      &lt;/div&gt;

  &lt;/div&gt;


  &lt;div class="dayscontainer clearfix pb-3"&gt;
    &lt;div class="row text-center dayhead mb-3"&gt;
      &lt;div class="col"&gt;M&lt;/div&gt;
      &lt;div class="col"&gt;T&lt;/div&gt;
      &lt;div class="col"&gt;W&lt;/div&gt;    
      &lt;div class="col"&gt;T&lt;/div&gt;
      &lt;div class="col"&gt;F&lt;/div&gt;
      &lt;div class="col"&gt;S&lt;/div&gt;
      &lt;div class="col"&gt;S&lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="row text-center mb-1"&gt;
        {% if year is defined and month is defined %}
            {% set time = (year ~ "-" ~ month ~ "-" ~ "01") %}
        {% endif%}
        {% if time is not defined%} 
            {% set time = "now" %}
        {% endif %}

            {% set daysInMonth = time|date('t') %}
            {% set startDow = time|date('F 1\\s\\t Y')|date('w') %}
            {% set dow = startDow %}



        {% set yearMonth = time|date("Y-m") %}                 

        {% set startdate=(yearMonth ~ "-" ~ "01") %}
        {% set enddate=(yearMonth ~ "-" ~ daysInMonth) %}
         {# Create our parameters object #}

        {% if (category is defined) and (category is not empty) %}
        {% set params = { section: 'events', orderBy: 'eventDate', relatedTo: category } %}
        {% else %}
        {% set params = { section: 'events', orderBy: 'eventDate' } %}
        {% endif %}          
        {% set j = 0 %}
        {# Retrieve all of the events for the month, and group them by day #}
        {% set groupedEvents = craft.entries(params).eventDate(['and', '&gt;= ' ~ startdate|date('Y-m-d'), '&lt;= ' ~ enddate|date('Y-m-d')]).all() | group('eventDate|date("Y-m-d")') %}



          {% for day in range(1,daysInMonth) %}
            {% set formattedDay = "%02d"|format(day) %}
            {% set dayOfMonth = yearMonth ~ "-" ~ formattedDay %}       

            {# set dayOfMonth = "#{yearMonth}-#{day}" #}
            {% if startDow == 0 %}
              {% set startDow = 7 %}
            {% endif %}


              {% if loop.first and startDow &gt; 1 %}
                {% for i in range(1,startDow-1) %}
                  {% set j = j + 1 %}
                  &lt;div class="col"&gt;&amp;nbsp;&lt;/div&gt;
                {% endfor %}              

              {% endif %}
              {% set j = j + 1 %}

              {% if j == 8 %}

                &lt;/div&gt;
                &lt;div class="row text-center mb-1"&gt;
                {% set j = 1 %}
              {% endif %}

              &lt;div class="col"&gt;

                {# If the current day is a key in our groupedEvents array, we have events #}
                {% if dayOfMonth in groupedEvents|keys %}
                  {% set ccontent %}
                      {% for entry in groupedEvents[dayOfMonth] %}
                      {% set category = entry.eventFilter.one() %}

                      &lt;span class="event"&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/span&gt;
                      {% endfor %}
                  {% endset %}

                  &lt;span class="day"&gt;&lt;a role="button" class="pop daylink" data-toggle="popover" data-trigger="click hover focus" data-html="true" title="Events" data-content='{{ ccontent }}'&gt;{{ day }}&lt;/a&gt;&lt;/span&gt;
                {% else %}
                  &lt;span class="day"&gt;{{ day }}&lt;/span&gt;  

                {% endif %}
              &lt;/div&gt;

              {% if loop.last and j &lt; 7 %}
                {% for i in range(1,7 -j) %}
                  &lt;div class="col"&gt;&amp;nbsp;&lt;/div&gt;
                {% endfor %}              

              {% endif %}              

          {% endfor %}
    &lt;/div&gt;
  &lt;/div&gt;
</code></pre>

CC BY-SA 4.0
2019-08-08T11:23:57.487Z
Thanks a lot! I will give this a try as soon as possible!
outline4
CC BY-SA 4.0
2019-08-08T08:01:04.680Z
2019-08-08T08:01:04.680Z
mmc501
<p>Thanks for this @mmc501
I have adapted the code to work the wonderful <a href="https://putyourlightson.com/plugins/sprig" rel="nofollow noreferrer">sprig</a> plugin.</p>
<pre><code>{% set time = time ?? &quot;now&quot; %}

&lt;div class=&quot;grid text-center&quot;&gt;
    &lt;div class=&quot;col&quot;&gt;
        &lt;button sprig s-vars=&quot;time: '{{time|date_modify('-1 month')|date()}}'&quot;&gt;Prev&lt;/button&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col&quot;&gt;
        &lt;div class=&quot;caldate&quot;&gt;{{ time|date(&quot;F&quot;)}} {{ time|date(&quot;Y&quot;)}}&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col&quot;&gt;
        &lt;button sprig s-vars=&quot;time: '{{time|date_modify('+1 month')|date()}}'&quot;&gt;Next&lt;/button&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;dayscontainer&quot;&gt;

    &lt;div class=&quot;grid text-center&quot;&gt;
        &lt;div class=&quot;col&quot;&gt;M&lt;/div&gt;
        &lt;div class=&quot;col&quot;&gt;T&lt;/div&gt;
        &lt;div class=&quot;col&quot;&gt;W&lt;/div&gt;
        &lt;div class=&quot;col&quot;&gt;T&lt;/div&gt;
        &lt;div class=&quot;col&quot;&gt;F&lt;/div&gt;
        &lt;div class=&quot;col&quot;&gt;S&lt;/div&gt;
        &lt;div class=&quot;col&quot;&gt;S&lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&quot;grid text-center&quot;&gt;
        {% set daysInMonth = time|date('t') %}
        {% set startDow = time|date('F 1\\s\\t Y')|date('w') %}

        {% set yearMonth = time|date(&quot;Y-m&quot;) %}                 

        {% set startdate=(yearMonth ~ &quot;-&quot; ~ &quot;01&quot;) %}
        {% set enddate=(yearMonth ~ &quot;-&quot; ~ daysInMonth) %}

        {% set params = { section: 'externalEvents', orderBy: 'startDateTime' } %}

        {% set j = 0 %}

        {% set groupedEvents = craft.entries(params).startDateTime(['and', '&gt;= ' ~ startdate|date('Y-m-d'), '&lt;= ' ~ enddate|date('Y-m-d')]).all()|group('startDateTime|date(&quot;Y-m-d&quot;)') %}

        {% for day in range(1,daysInMonth) %}
            {% set formattedDay = &quot;%02d&quot;|format(day) %}
            {% set dayOfMonth = yearMonth ~ &quot;-&quot; ~ formattedDay %}       

            {% if startDow == 0 %}
                {% set startDow = 7 %}
            {% endif %}

            {% if loop.first and startDow &gt; 1 %}
                {% for i in range(1,startDow-1) %}
                    {% set j = j + 1 %}
                    &lt;div class=&quot;col&quot;&gt;&lt;/div&gt;
                {% endfor %}              
            {% endif %}

            {% set j = j + 1 %}

            {% if j == 8 %}
                &lt;/div&gt;
                &lt;div class=&quot;grid text-center&quot;&gt;
                {% set j = 1 %}
            {% endif %}

            &lt;div class=&quot;col&quot;&gt;
                {% if dayOfMonth in groupedEvents|keys %}
                    {% for entry in groupedEvents[dayOfMonth] %}
                        &lt;a href=&quot;{{ entry.url }}&quot;&gt;{{ entry.title }}&lt;/a&gt;
                    {% endfor %}
                    {{ day }}
                {% else %}
                    {{ day }}
                {% endif %}
            &lt;/div&gt;

            {% if loop.last and j &lt; 7 %}
                {% for i in range(1,7 -j) %}
                    &lt;div class=&quot;col&quot;&gt;&lt;/div&gt;
                {% endfor %}
            {% endif %}

        {% endfor %}
    &lt;/div&gt;
&lt;/div&gt;
</code></pre>

CC BY-SA 4.0
2020-10-12T17:39:33.883Z
2020-10-12T17:39:33.883Z
William Hibberd
<p>I haven't tested this, but it should be a good starting point, at the very least.</p>

<pre><code>{# Determine the number of days in the month #}
{% set daysInMonth = "now"|date("t") %}

{# Determine the date range spanning the month #}
{% set firstDayOfMonth = "now"|date("Y-m-01") %}
{% set firstDayOfNextMonth = "now"|date_modify("first day of next month")|date("Y-m-01") %}
{% set postDateParam = "and, &gt;= " ~ firstDayOfMonth ~ ", &lt; " ~ firstDayOfNextMonth %}

{# Create our parameters object #}
{% if (category is defined) and (category is not empty) %}
    {% set params = {section: 'calendar', postDate: postDateParam, status: null, order: 'postDate', relatedTo: category} %}
{% else %}
    {% set params = {section: 'calendar', postDate: postDateParam, status: null, order: 'postDate'} %}
{% endif %}

{# Retrieve all of the events for the month, and group them by day #}
{% set groupedEvents = craft.entries(params)|group('postDate|date("Y-m-d")') %}

{# Set a shortcut for the YYYY-MM portion of the current month #}
{% set yearMonth = "now"|date("Y-m") %}

{# Loop through all of the days in the month #}
{% for day in range(1, daysInMonth) %}
    {% set formattedDay = "%02d"|format(day) %}
    {% set dayOfMonth = yearMonth ~ "-" ~ formattedDay) %}

    {# If the current day is a key in our groupedEvents array, we have events #}
    {% if dayOfMonth in groupedEvents|keys %}
        &lt;span class="day"&gt;{{ dayOfMonth }}&lt;/span&gt;

        &lt;div&gt;
            {% for event in groupedEvents[dayOfMonth] %}
                &lt;span class="event green"&gt;{{ event.title }}&lt;/span&gt;
            {% endfor %}
        &lt;/div&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>As I said, I haven't tested the above code, but the basic principle works (I've used it for grouping people by surname, and so forth).</p>

<p>Let me know if anything is unclear, I realise it's quite the chunk of Twig.</p>

<p>Stephen</p>

CC BY-SA 3.0
2014-08-12T20:48:40.160Z
Stephen, you need to change the date filtering to get all entries within a datetime range. See [this Q/A](http://craftcms.stackexchange.com/q/1821/125) for more info.
carlcs
CC BY-SA 3.0
2014-08-15T06:22:48.570Z
Thanks Stephen - works well - needed to change the date filtering as carlcs said.
But I'm not getting all events pulled in - missing one on the 1st of the month. will add code below
mmc501
CC BY-SA 3.0
2014-08-15T08:55:38.437Z
@mmc501 when did you add your events to Craft? There were a couple of timezone related bugs which got fixed with Craft version [2.0.2528](http://buildwithcraft.com/updates#build2528), [1.2.2375](http://buildwithcraft.com/updates#build2375) and [0.9.2235](http://buildwithcraft.com/updates#build2235). I'd try to edit the date of that missing event on the 1st of the month with the timepicker (change it to the 2nd → save, change it back to the 1st → save).
carlcs
CC BY-SA 3.0
2014-08-15T09:54:54.287Z
Within the last couple of weeks.
Tried editing the date of the event - discovered that if it's the 1st - 9th of the month it will not display but from the 10th on it will - must be something to do with single figure date - will have a play with it not to see if can resolve.
mmc501
CC BY-SA 3.0
2014-08-15T10:35:03.823Z
@mmc501 I've updated my example to format the day as a two-digit number (take a look at the first line of the `for day in range` loop).
Stephen Lewis
CC BY-SA 3.0
2014-08-15T10:39:41.133Z
@carlcs Thanks for fixing the date range query.
Stephen Lewis
CC BY-SA 3.0
2014-08-15T10:47:45.463Z
Excellent! - working great now! Much better than my original solution with a query for every day.
mmc501
CC BY-SA 3.0
2014-08-15T10:50:30.847Z
@mmc501 Nice one. We got there eventually.
Stephen Lewis
CC BY-SA 3.0
2014-08-15T11:05:48.693Z
Was a pleasure to contribute to this nice Q/A, @stephen. I'm sure that I will use this code snippet at some point! One question (and please correct it with the code), what's the right or most common way to say it: timeframe, date range, interval?
carlcs
CC BY-SA 3.0
2014-08-15T11:27:50.390Z
@carlcs In this case I'd be inclined to go with timeframe or date range. Interval suggests a regular, repeating period of time.
Stephen Lewis
CC BY-SA 3.0
2014-08-15T11:30:28.277Z
@carlcs Having said that, I've just updated the example to use the rather more prosaic (but less potentially confusing) `postDateParam`.
Stephen Lewis
CC BY-SA 3.0
2014-08-15T18:26:54.163Z
@mmc501 didn't you miss events that are on the last calendar day? See my edit to the code..
carlcs
CC BY-SA 3.0
2014-08-18T05:44:34.567Z
Cheers @carlcs - hadn't tested an entry on the last day of the month - updated to your edit now, thanks!
mmc501
CC BY-SA 3.0
2014-08-12T19:57:50.907Z
2015-03-04T21:10:00.127Z
2015-03-04T21:10:00.127Z
Stephen Lewis
<p>I have a section for holding calendar entries stored by postDate in the CMS.
On the front end I want to basically display these in a calendar.</p>

<p>I have code which displays the calendar looping through each day of the month.
For each day it currently queries if there is any entries for this day.</p>

<p>Is there a better way to do it? - my current method had a query for each day?</p>

<p>Current code is:</p>

<pre><code>&lt;table class="cal"&gt;
  &lt;thead class="hide-for-small"&gt;
    &lt;tr&gt;
      &lt;th width="300"&gt;Sun&lt;/th&gt;
      &lt;th width="300"&gt;Mon&lt;/th&gt;
      &lt;th width="300"&gt;Tue&lt;/th&gt;
      &lt;th width="300"&gt;Wed&lt;/th&gt;
      &lt;th width="300"&gt;Thu&lt;/th&gt;
      &lt;th width="300"&gt;Fri&lt;/th&gt;
      &lt;th width="300"&gt;Sat&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
    {% set time = "now"|date("U") %}        
    {% set daysInMonth = time|date('t') %}
    {% set startDow = time|date('F 1\\s\\t Y')|date('w') %}
    {% set dow = startDow %}
    {% for day in range(1,daysInMonth) %}
        {% if loop.first and startDow != 0 %}
            &lt;td colspan="{{ startDow }}"&gt;&lt;/td&gt;
        {% endif %}
        &lt;td&gt;
            &lt;div&gt;
                &lt;span class="day"&gt;{{ day }}&lt;/span&gt;
                    {% set today=thisyear ~ "/" ~ thismonth ~ "/" ~ day %}

                    {% if (category is defined) and (category is not empty) %}
                    {% set params = { section: 'calendar', status: null, postDate:today|date('Y-m-d'), relatedTo: category } %}
                    {% else %}
                    {% set params = { section: 'calendar', status: null, postDate:today|date('Y-m-d') } %}
                    {% endif %}
                    {% set entries = craft.entries(params)%}
                    {% if entries|length %}
                &lt;div&gt;
                    {% for entry in entries %}
                    &lt;span class="event green"&gt;{{ entry.title }}&lt;/span&gt;
                    {% endfor %}
                &lt;/div&gt;
                    {% endif %}
            &lt;/div&gt;
        &lt;/td&gt;
        {% if loop.last and dow != 6 %}
            &lt;td colspan="{{ 6 - dow }}"&gt;&amp;nbsp;&lt;/td&gt;
        {% endif %}
        {% if dow == 6 %}
            {% set dow = 0 %}
        &lt;/tr&gt;
        &lt;tr&gt;
        {% else %}
            {% set dow = dow + 1 %}
        {% endif %}
    {% endfor %}
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;  
</code></pre>

CC BY-SA 4.0
2019-07-09T09:13:03.430Z
Hi mmc501. Did you ever finish the code with the answer from Stephen Lewis? I am looking to output a mini calendar with prev/next month etc. any help appreciated!
outline4
CC BY-SA 4.0
2019-07-09T12:58:40.100Z
@outline4 yes I finished it and have a working version. Can get it when I return from holidays next week
mmc501
CC BY-SA 4.0
2019-07-09T16:11:46.997Z
That'd be great!
outline4
CC BY-SA 4.0
2019-08-07T12:47:23.003Z
are you back from your holidays? cheers stefan
outline4
CC BY-SA 4.0
2019-08-08T07:58:31.850Z
Yes - I will post it as an additional answer
mmc501
<entries><date><postdate><queries>
Display Entries in a calendar
# Display Entries in a calendar

(Asked by: username_0 on 2014-08-12)

I have a section for holding calendar entries stored by postDate in the CMS.
On the front end I want to basically display these in a calendar.
I have code which displays the calendar looping through each day of the month.
For each day it currently queries if there is any entries for this day.
Is there a better way to do it? - my current method had a query for each day?
Current code is:

  
    
      Sun
      Mon
      Tue
      Wed
      Thu
      Fri
      Sat
    
  
  
    
    {% set time = "now"|date("U") %}        
    {% set daysInMonth = time|date('t') %}
    {% set startDow = time|date('F 1\\s\\t Y')|date('w') %}
    {% set dow = startDow %}
    {% for day in range(1,daysInMonth) %}
        {% if loop.first and startDow != 0 %}
            
        {% endif %}
        
            
                {{ day }}
                    {% set today=thisyear ~ "/" ~ thismonth ~ "/" ~ day %}

                    {% if (category is defined) and (category is not empty) %}
                    {% set params = { section: 'calendar', status: null, postDate:today|date('Y-m-d'), relatedTo: category } %}
                    {% else %}
                    {% set params = { section: 'calendar', status: null, postDate:today|date('Y-m-d') } %}
                    {% endif %}
                    {% set entries = craft.entries(params)%}
                    {% if entries|length %}
                
                    {% for entry in entries %}
                    {{ entry.title }}
                    {% endfor %}
                
                    {% endif %}
            
        
        {% if loop.last and dow != 6 %}
             
        {% endif %}
        {% if dow == 6 %}
            {% set dow = 0 %}
        
        
        {% else %}
            {% set dow = dow + 1 %}
        {% endif %}
    {% endfor %}

**Tags:** entries, date, postdate, queries
> **Comments:**
> **username_1**: Hi mmc501. Did you ever finish the code with the answer from Stephen Lewis? I am looking to output a mini calendar with prev/next month etc. any help appreciated!  
> **username_0**: @outline4 yes I finished it and have a working version. Can get it when I return from holidays next week  
> **username_1**: That'd be great!  
> **username_1**: are you back from your holidays? cheers stefan  
> **username_0**: Yes - I will post it as an additional answer  

---

## 3 answers

### Answer by username_3 on 2014-08-12. Score: 9 | Accepted answer

I haven't tested this, but it should be a good starting point, at the very least.
{# Determine the number of days in the month #}
{% set daysInMonth = "now"|date("t") %}

{# Determine the date range spanning the month #}
{% set firstDayOfMonth = "now"|date("Y-m-01") %}
{% set firstDayOfNextMonth = "now"|date_modify("first day of next month")|date("Y-m-01") %}
{% set postDateParam = "and, >= " ~ firstDayOfMonth ~ ", < " ~ firstDayOfNextMonth %}

{# Create our parameters object #}
{% if (category is defined) and (category is not empty) %}
    {% set params = {section: 'calendar', postDate: postDateParam, status: null, order: 'postDate', relatedTo: category} %}
{% else %}
    {% set params = {section: 'calendar', postDate: postDateParam, status: null, order: 'postDate'} %}
{% endif %}

{# Retrieve all of the events for the month, and group them by day #}
{% set groupedEvents = craft.entries(params)|group('postDate|date("Y-m-d")') %}

{# Set a shortcut for the YYYY-MM portion of the current month #}
{% set yearMonth = "now"|date("Y-m") %}

{# Loop through all of the days in the month #}
{% for day in range(1, daysInMonth) %}
    {% set formattedDay = "%02d"|format(day) %}
    {% set dayOfMonth = yearMonth ~ "-" ~ formattedDay) %}

    {# If the current day is a key in our groupedEvents array, we have events #}
    {% if dayOfMonth in groupedEvents|keys %}
        {{ dayOfMonth }}

        
            {% for event in groupedEvents[dayOfMonth] %}
                {{ event.title }}
            {% endfor %}
        
    {% endif %}
{% endfor %}

As I said, I haven't tested the above code, but the basic principle works (I've used it for grouping people by surname, and so forth).
Let me know if anything is unclear, I realise it's quite the chunk of Twig.
Stephen

> **Comments:**
> **username_4**: Stephen, you need to change the date filtering to get all entries within a datetime range. See [this Q/A](http://craftcms.stackexchange.com/q/1821/125) for more info.  
> **username_0**: Thanks Stephen - works well - needed to change the date filtering as carlcs said. But I'm not getting all events pulled in - missing one on the 1st of the month. will add code below  
> **username_4**: @mmc501 when did you add your events to Craft? There were a couple of timezone related bugs which got fixed with Craft version [2.0.2528](http://buildwithcraft.com/updates#build2528), [1.2.2375](http://buildwithcraft.com/updates#build2375) and [0.9.2235](http://buildwithcraft.com/updates#build2235). I'd try to edit the date of that missing event on the 1st of the month with the timepicker (change it to the 2nd → save, change it back to the 1st → save).  
> **username_0**: Within the last couple of weeks. Tried editing the date of the event - discovered that if it's the 1st - 9th of the month it will not display but from the 10th on it will - must be something to do with single figure date - will have a play with it not to see if can resolve.  
> **username_3**: @mmc501 I've updated my example to format the day as a two-digit number (take a look at the first line of the `for day in range` loop).  
> **username_3**: @carlcs Thanks for fixing the date range query.  
> **username_0**: Excellent! - working great now! Much better than my original solution with a query for every day.  
> **username_3**: @mmc501 Nice one. We got there eventually.  
> **username_4**: Was a pleasure to contribute to this nice Q/A, @stephen. I'm sure that I will use this code snippet at some point! One question (and please correct it with the code), what's the right or most common way to say it: timeframe, date range, interval?  
> **username_3**: @carlcs In this case I'd be inclined to go with timeframe or date range. Interval suggests a regular, repeating period of time.  
> **username_3**: @carlcs Having said that, I've just updated the example to use the rather more prosaic (but less potentially confusing) `postDateParam`.  
> **username_4**: @mmc501 didn't you miss events that are on the last calendar day? See my edit to the code..  
> **username_0**: Cheers @carlcs - hadn't tested an entry on the last day of the month - updated to your edit now, thanks!  

---

### Answer by username_0 on 2019-08-08. Score: 2

Just an update to show final current version of code for calendar - re stefan comment.
  {% if year is defined and month is defined %}
      {% set time = (year ~ "-" ~ month ~ "-" ~ "01") %}
      {% set catslug = craft.app.request.segment(2) %}
      {% if catslug != 'all' %}
        {% set category = craft.categories.group('eventFilters').slug(catslug) %}
      {% endif %}
  {% endif %}
  {% if time is not defined%} 
      {% set time = "now" %}
  {% endif %}
  {% if catslug is not defined or catslug is empty%} 
      {% set catslug = "all" %}
  {% endif %}

  {% set thisyear = time|date("Y") %}
  {% set thismonth = time|date("m") %}
  {% set prevyear = time|date_modify("-1 month")|date("Y") %}
  {% set prevmonth = time|date_modify("-1 month")|date("m") %}
  {% set nextyear = time|date_modify("+1 month")|date("Y") %}
  {% set nextmonth = time|date_modify("+1 month")|date("m") %}



  

  
      
        
          
            
            
            
        
      

      {{ time|date("F")}} {{ time|date("Y")}}
      
        
          
            
            
                    
        
      

  


  
    
      M
      T
      W    
      T
      F
      S
      S
    

    
        {% if year is defined and month is defined %}
            {% set time = (year ~ "-" ~ month ~ "-" ~ "01") %}
        {% endif%}
        {% if time is not defined%} 
            {% set time = "now" %}
        {% endif %}

            {% set daysInMonth = time|date('t') %}
            {% set startDow = time|date('F 1\\s\\t Y')|date('w') %}
            {% set dow = startDow %}



        {% set yearMonth = time|date("Y-m") %}                 

        {% set startdate=(yearMonth ~ "-" ~ "01") %}
        {% set enddate=(yearMonth ~ "-" ~ daysInMonth) %}
         {# Create our parameters object #}

        {% if (category is defined) and (category is not empty) %}
        {% set params = { section: 'events', orderBy: 'eventDate', relatedTo: category } %}
        {% else %}
        {% set params = { section: 'events', orderBy: 'eventDate' } %}
        {% endif %}          
        {% set j = 0 %}
        {# Retrieve all of the events for the month, and group them by day #}
        {% set groupedEvents = craft.entries(params).eventDate(['and', '>= ' ~ startdate|date('Y-m-d'), '<= ' ~ enddate|date('Y-m-d')]).all() | group('eventDate|date("Y-m-d")') %}



          {% for day in range(1,daysInMonth) %}
            {% set formattedDay = "%02d"|format(day) %}
            {% set dayOfMonth = yearMonth ~ "-" ~ formattedDay %}       

            {# set dayOfMonth = "#{yearMonth}-#{day}" #}
            {% if startDow == 0 %}
              {% set startDow = 7 %}
            {% endif %}


              {% if loop.first and startDow > 1 %}
                {% for i in range(1,startDow-1) %}
                  {% set j = j + 1 %}
                   
                {% endfor %}              

              {% endif %}
              {% set j = j + 1 %}

              {% if j == 8 %}

                
                
                {% set j = 1 %}
              {% endif %}

              

                {# If the current day is a key in our groupedEvents array, we have events #}
                {% if dayOfMonth in groupedEvents|keys %}
                  {% set ccontent %}
                      {% for entry in groupedEvents[dayOfMonth] %}
                      {% set category = entry.eventFilter.one() %}

                      {{ entry.title }}
                      {% endfor %}
                  {% endset %}

                  {{ day }}
                {% else %}
                  {{ day }}  

                {% endif %}
              

              {% if loop.last and j < 7 %}
                {% for i in range(1,7 -j) %}
                   
                {% endfor %}              

              {% endif %}              

          {% endfor %}

> **Comments:**
> **username_1**: Thanks a lot! I will give this a try as soon as possible!  

---

### Answer by username_2 on 2020-10-12. Score: 2

Thanks for this @mmc501
I have adapted the code to work the wonderful sprig plugin.
{% set time = time ?? "now" %}


    
        Prev
    
    
        {{ time|date("F")}} {{ time|date("Y")}}
    
    
        Next
    




    
        M
        T
        W
        T
        F
        S
        S
    

    
        {% set daysInMonth = time|date('t') %}
        {% set startDow = time|date('F 1\\s\\t Y')|date('w') %}

        {% set yearMonth = time|date("Y-m") %}                 

        {% set startdate=(yearMonth ~ "-" ~ "01") %}
        {% set enddate=(yearMonth ~ "-" ~ daysInMonth) %}

        {% set params = { section: 'externalEvents', orderBy: 'startDateTime' } %}

        {% set j = 0 %}

        {% set groupedEvents = craft.entries(params).startDateTime(['and', '>= ' ~ startdate|date('Y-m-d'), '<= ' ~ enddate|date('Y-m-d')]).all()|group('startDateTime|date("Y-m-d")') %}

        {% for day in range(1,daysInMonth) %}
            {% set formattedDay = "%02d"|format(day) %}
            {% set dayOfMonth = yearMonth ~ "-" ~ formattedDay %}       

            {% if startDow == 0 %}
                {% set startDow = 7 %}
            {% endif %}

            {% if loop.first and startDow > 1 %}
                {% for i in range(1,startDow-1) %}
                    {% set j = j + 1 %}
                    
                {% endfor %}              
            {% endif %}

            {% set j = j + 1 %}

            {% if j == 8 %}
                
                
                {% set j = 1 %}
            {% endif %}

            
                {% if dayOfMonth in groupedEvents|keys %}
                    {% for entry in groupedEvents[dayOfMonth] %}
                        {{ entry.title }}
                    {% endfor %}
                    {{ day }}
                {% else %}
                    {{ day }}
                {% endif %}
            

            {% if loop.last and j < 7 %}
                {% for i in range(1,7 -j) %}
                    
                {% endfor %}
            {% endif %}

        {% endfor %}


---

