Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-08T11:23:57.487Z
Answers.Text: Thanks a lot! I will give this a try as soon as possible!
Answers.UserDisplayName: outline4
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-08T08:01:04.680Z
Answers.LastActivityDate: 2019-08-08T08:01:04.680Z
Answers.UserDisplayName: mmc501
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-12T17:39:33.883Z
Answers.LastActivityDate: 2020-10-12T17:39:33.883Z
Answers.UserDisplayName: William Hibberd
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-12T20:48:40.160Z
Answers.Text: 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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T06:22:48.570Z
Answers.Text: 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
Answers.UserDisplayName: mmc501
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T08:55:38.437Z
Answers.Text: @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).
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T09:54:54.287Z
Answers.Text: 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.
Answers.UserDisplayName: mmc501
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T10:35:03.823Z
Answers.Text: @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).
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T10:39:41.133Z
Answers.Text: @carlcs Thanks for fixing the date range query.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T10:47:45.463Z
Answers.Text: Excellent! - working great now! Much better than my original solution with a query for every day.
Answers.UserDisplayName: mmc501
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T10:50:30.847Z
Answers.Text: @mmc501 Nice one. We got there eventually.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T11:05:48.693Z
Answers.Text: 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?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T11:27:50.390Z
Answers.Text: @carlcs In this case I'd be inclined to go with timeframe or date range. Interval suggests a regular, repeating period of time.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T11:30:28.277Z
Answers.Text: @carlcs Having said that, I've just updated the example to use the rather more prosaic (but less potentially confusing) `postDateParam`.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T18:26:54.163Z
Answers.Text: @mmc501 didn't you miss events that are on the last calendar day? See my edit to the code..
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-18T05:44:34.567Z
Answers.Text: Cheers @carlcs - hadn't tested an entry on the last day of the month - updated to your edit now, thanks!
Answers.UserDisplayName: mmc501
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-12T19:57:50.907Z
Answers.LastActivityDate: 2015-03-04T21:10:00.127Z
Answers.LastEditDate: 2015-03-04T21:10:00.127Z
Answers.UserDisplayName: Stephen Lewis
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-09T09:13:03.430Z
Comments.Text: 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!
Comments.UserDisplayName: outline4
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-09T12:58:40.100Z
Comments.Text: @outline4 yes I finished it and have a working version. Can get it when I return from holidays next week
Comments.UserDisplayName: mmc501
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-09T16:11:46.997Z
Comments.Text: That'd be great!
Comments.UserDisplayName: outline4
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-07T12:47:23.003Z
Comments.Text: are you back from your holidays? cheers stefan
Comments.UserDisplayName: outline4
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-08T07:58:31.850Z
Comments.Text: Yes - I will post it as an additional answer
Comments.UserDisplayName: mmc501
Tags: <entries><date><postdate><queries>
Title: Display Entries in a calendar
ThreadText: # 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:
  
  ```
  <table class="cal">
    <thead class="hide-for-small">
      <tr>
        <th width="300">Sun</th>
        <th width="300">Mon</th>
        <th width="300">Tue</th>
        <th width="300">Wed</th>
        <th width="300">Thu</th>
        <th width="300">Fri</th>
        <th width="300">Sat</th>
      </tr>
    </thead>
    <tbody>
      <tr>
      {% 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 %}
              <td colspan="{{ startDow }}"></td>
          {% endif %}
          <td>
              <div>
                  <span class="day">{{ day }}</span>
                      {% 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 %}
                  <div>
                      {% for entry in entries %}
                      <span class="event green">{{ entry.title }}</span>
                      {% endfor %}
                  </div>
                      {% endif %}
              </div>
          </td>
          {% if loop.last and dow != 6 %}
              <td colspan="{{ 6 - dow }}">&nbsp;</td>
          {% endif %}
          {% if dow == 6 %}
              {% set dow = 0 %}
          </tr>
          <tr>
          {% else %}
              {% set dow = dow + 1 %}
          {% endif %}
      {% endfor %}
      </tr>
    </tbody>
  </table>  
  
  ```
  
  **Tags:** entries, date, postdate, queries
  
  > **Comments:**
  > **username_1**: Hi username_0. Did you ever finish the code with the answer from username_3? I am looking to output a mini calendar with prev/next month etc. any help appreciated!  
  > **username_0**: @username_1 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 %}
          <span class="day">{{ dayOfMonth }}</span>
  
          <div>
              {% for event in groupedEvents[dayOfMonth] %}
                  <span class="event green">{{ event.title }}</span>
              {% endfor %}
          </div>
      {% 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.
  
  username_3
  
  > **Comments:**
  > **username_4**: username_3, 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 username_3 - works well - needed to change the date filtering as username_4 said. But I'm not getting all events pulled in - missing one on the 1st of the month. will add code below  
  > **username_4**: @username_0 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**: @username_0 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**: @username_4 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**: @username_0 Nice one. We got there eventually.  
  > **username_4**: Was a pleasure to contribute to this nice Q/A, @username_3. 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**: @username_4 In this case I'd be inclined to go with timeframe or date range. Interval suggests a regular, repeating period of time.  
  > **username_3**: @username_4 Having said that, I've just updated the example to use the rather more prosaic (but less potentially confusing) `postDateParam`.  
  > **username_4**: @username_0 didn't you miss events that are on the last calendar day? See my edit to the code..  
  > **username_0**: Cheers @username_4 - 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") %}
  
    <!-- CONTENT -->
  
    <div class="row calhead text-center align-items-center py-2 mb-3">
        <div class="col-auto">
          <a class="calchange calleft" href="#" data-slug="{{ catslug }}" data-month="{{ prevmonth }}" data-year="{{ prevyear }}">
            <span class="fa-stack ">
              <i class="fas fa-circle fa-stack-2x"></i>
              <i class="fas fa-angle-left fa-stack-1x fa-inverse"></i>
            </span>  
          </a>
        </div>
  
        <div class="col"><div class="caldate">{{ time|date("F")}} {{ time|date("Y")}}</div></div>
        <div class="col-auto">
          <a class="calchange calright" href="#" data-slug="{{ catslug }}" data-month="{{ nextmonth }}" data-year="{{ nextyear }}">
            <span class="fa-stack ">
              <i class="fas fa-circle fa-stack-2x"></i>
              <i class="fas fa-angle-right fa-stack-1x fa-inverse"></i>
            </span>          
          </a>
        </div>
  
    </div>
  
    <div class="dayscontainer clearfix pb-3">
      <div class="row text-center dayhead mb-3">
        <div class="col">M</div>
        <div class="col">T</div>
        <div class="col">W</div>    
        <div class="col">T</div>
        <div class="col">F</div>
        <div class="col">S</div>
        <div class="col">S</div>
      </div>
  
      <div class="row text-center mb-1">
          {% 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 %}
                    <div class="col">&nbsp;</div>
                  {% endfor %}              
  
                {% endif %}
                {% set j = j + 1 %}
  
                {% if j == 8 %}
  
                  </div>
                  <div class="row text-center mb-1">
                  {% set j = 1 %}
                {% endif %}
  
                <div class="col">
  
                  {# 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() %}
  
                        <span class="event"><a href="{{ entry.url }}">{{ entry.title }}</a></span>
                        {% endfor %}
                    {% endset %}
  
                    <span class="day"><a role="button" class="pop daylink" data-toggle="popover" data-trigger="click hover focus" data-html="true" title="Events" data-content='{{ ccontent }}'>{{ day }}</a></span>
                  {% else %}
                    <span class="day">{{ day }}</span>  
  
                  {% endif %}
                </div>
  
                {% if loop.last and j < 7 %}
                  {% for i in range(1,7 -j) %}
                    <div class="col">&nbsp;</div>
                  {% endfor %}              
  
                {% endif %}              
  
            {% endfor %}
      </div>
    </div>
  
  ```
  
  > **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 @username_0
  I have adapted the code to work the wonderful [sprig](https://putyourlightson.com/plugins/sprig) plugin.
  
  ```
  {% set time = time ?? "now" %}
  
  <div class="grid text-center">
      <div class="col">
          <button sprig s-vars="time: '{{time|date_modify('-1 month')|date()}}'">Prev</button>
      </div>
      <div class="col">
          <div class="caldate">{{ time|date("F")}} {{ time|date("Y")}}</div>
      </div>
      <div class="col">
          <button sprig s-vars="time: '{{time|date_modify('+1 month')|date()}}'">Next</button>
      </div>
  </div>
  
  <div class="dayscontainer">
  
      <div class="grid text-center">
          <div class="col">M</div>
          <div class="col">T</div>
          <div class="col">W</div>
          <div class="col">T</div>
          <div class="col">F</div>
          <div class="col">S</div>
          <div class="col">S</div>
      </div>
  
      <div class="grid text-center">
          {% 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 %}
                      <div class="col"></div>
                  {% endfor %}              
              {% endif %}
  
              {% set j = j + 1 %}
  
              {% if j == 8 %}
                  </div>
                  <div class="grid text-center">
                  {% set j = 1 %}
              {% endif %}
  
              <div class="col">
                  {% if dayOfMonth in groupedEvents|keys %}
                      {% for entry in groupedEvents[dayOfMonth] %}
                          <a href="{{ entry.url }}">{{ entry.title }}</a>
                      {% endfor %}
                      {{ day }}
                  {% else %}
                      {{ day }}
                  {% endif %}
              </div>
  
              {% if loop.last and j < 7 %}
                  {% for i in range(1,7 -j) %}
                      <div class="col"></div>
                  {% endfor %}
              {% endif %}
  
          {% endfor %}
      </div>
  </div>
  
  ```
  
  
  ---
  
  