Answers.Body: <p>Instead of using pagination you might want to look into setting up a <a href="http://buildwithcraft.com/docs/routing#dynamic-routes" rel="nofollow noreferrer">dynamic route</a> in the format "calendar/[year]/[month]". Adding 'year' and 'month' tags to your route will essentially pass those values as named variables to your template, which you can use to retrieve and display entries, as well as generate navigation to next/previous month's events. If you want the uri '/calendar' (without the 'year/month' uri segments) to link to the current months events you can test for the existence of the 'year' and 'month' variables, and if not defined, set the start month to the first day of the current month.</p>
  
  <pre><code>{% if year is defined and month is defined %}
      {% set startMonth = year ~ '-' ~ month ~ '-' ~ 1 %}
  {% else %}
      {% set startMonth = now|date_modify("first day of this month") %}
  {% endif %}
  {% set nextMonth = startMonth|date_modify('+1 month')) %}
  {% set previousMonth = startMonth|date_modify('-1 month')) %}
  
  {% set entries = craft.entries.section('calendar').postDate('and', '&gt;= ' ~ startMonth, '&lt; ' ~ nextMonth %}
  
  {% for entry in entries %}
      {{ entry.title }}
      ...
  {% endfor %}
  
  &lt;div class="pagination"&gt;
      &lt;a href="{{ 'events/' ~ nextMonth|date("Y/n") }}"&gt;next month&lt;/a&gt;
      &lt;a href="{{ 'events/' ~ previousMonth|date("Y/n") }} "&gt;previous month&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>If required, within any month you could also use a pagination tag if you needed to limit the number of events displayed on any one page to 15.</p>
  
  <p>For additional examples see these answers, <a href="https://craftcms.stackexchange.com/a/5456/784">here</a> and <a href="https://craftcms.stackexchange.com/a/9527/784">here</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-26T07:30:12.120Z
Answers.Text: Thanks for the example. I've never used Routes before. How would my actual route look like when the calendar in question is an include that is part of my homepage?
  
  Would it be "calendar/year/month" ? Which template should I specify it to load then? Since it's an include, and not a template in the way I think of a template?
Answers.UserDisplayName: Filadelfia
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-26T08:02:54.150Z
Answers.Text: Also, curious if it's possible to actually display the month name when paginating backward/forward? So instead of saying "Previous Month", it would say "May".
  
  BTW: your current code works perfectly, except for the previous/next links, because of the Route part not being correct set up.
Answers.UserDisplayName: Filadelfia
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-27T16:42:43.803Z
Answers.Text: Sorry, I think the month number without the leading zero is represented by 'n' instead of 'm'. I'll update the answer. See the [php date docs](http://php.net/manual/en/function.date.php) for more info. To output the month name, I believe you can use 'F': `<a href="{{ 'events/' ~ nextMonth|date("Y/n") }}">{{ nextMonth|date("F") }}</a>`
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-27T16:57:58.733Z
Answers.Text: Regarding the include question — if your looking to include events on the homepage, you might want to consider using ajax to load the calendar (to keep the uri's clean). Or perhaps consider having the "hompage" calendar (which shows the current month's events) link to the full calendar for previous/future events. Otherwise, if you want to incorporate routes with the homepage you would probably want to drop the 'calendar' segment of the route and just use `[year]/[month]` as your route, and specify the homepage template as the route template. The calendar could still be a template include.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-25T16:05:40.230Z
Answers.LastActivityDate: 2015-06-27T16:43:04.140Z
Answers.LastEditDate: 2017-04-13T12:51:00.253Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I'm working on creating a simple calendar based on only native Craft fields. While coding this, I'm having a hard time getting the pagination to work properly.</p>
  
  <p>The calendar currently lists all upcoming events this current month. Next/Previous should then paginate the calendar 1 full month forward/backward (not 1 month backwards from todays date, but all entries in the next/previous month).</p>
  
  <p>According to Brandon, paginate.prevUrl/nextUrl won't map to specific date-based starting points. So, how do I tackle this problem then?</p>
  
  <p>Here's my current code:</p>
  
  <pre><code>&lt;div class="row"&gt;
      &lt;div class="small-12 medium-12 large-12 columns"&gt;
          &lt;div class="widget_event single_widget box_shadow"&gt;
              &lt;h3&gt;Hva skjer i Filadelfia&lt;/h3&gt;
  
              {% set startDateTime = now|date_modify('0:00')|date('c') %}
              {% set endDateTime = now|date_modify('first day of next month 4:00')|date('c') %}
  
              {% set startDateParam = 'and, &gt;= ' ~ startDateTime ~ ', &lt; ' ~ endDateTime %}
  
              {% set events = craft.entries.section('calendar').limit('15').startDate(startDateParam).order('startDate asc') %}
  
  {% for event in events %}
      &lt;div class="event_list"&gt;
          &lt;div class="single_event"&gt;
  
              &lt;div class="date_left"&gt;
                  &lt;p class="date"&gt;{{ event.startDate | date("d") }}.&lt;/p&gt;
                  &lt;p class="day"&gt;{{ event.startDate | date("M") }}&lt;/p&gt;
              &lt;/div&gt;
  
              &lt;div class="title_middle"&gt;
                  &lt;p class="title"&gt;{{ event.title }}&lt;/p&gt;
                  &lt;p class="desc"&gt;{{ event.body | hacksaw(words='75', allow='&lt;b&gt;&lt;strong&gt;&lt;i&gt;&lt;em&gt;') }}&lt;/p&gt;
              &lt;/div&gt;
  
              &lt;div class="time"&gt;{{ event.startDate | date("H:i") }}&lt;/div&gt;
  
          &lt;/div&gt;
      &lt;/div&gt;
  {% endfor %}
  
  
  {% paginate craft.entries.section('calendar').limit(15) as pageEvents %}
      {% for month, entries in pageEvents | group("postDate | date('F')") %}
          &lt;div class="event_nav text-center"&gt;
              &lt;span class="left left_arrow"&gt;{% if paginate.prevUrl %}&lt;a href="{{ paginate.prevUrl }}"&gt;Previous Month&lt;/a&gt;{% endif %}&lt;/span&gt;
              &lt;span class="right right_arrow"&gt;{% if paginate.nextUrl %}&lt;a href="{{ paginate.nextUrl }}"&gt;Next Month&lt;/a&gt;{% endif %}&lt;/span&gt;
          &lt;/div&gt;
      {% endfor %}
  {% endpaginate %}
  
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
Tags: <pagination>
Title: Paginate based on date-based starting points
ThreadText: # Paginate based on date-based starting points
  
  (Asked by: username_0 on 2015-06-25)
  
  I'm working on creating a simple calendar based on only native Craft fields. While coding this, I'm having a hard time getting the pagination to work properly.
  
  The calendar currently lists all upcoming events this current month. Next/Previous should then paginate the calendar 1 full month forward/backward (not 1 month backwards from todays date, but all entries in the next/previous month).
  
  According to Brandon, paginate.prevUrl/nextUrl won't map to specific date-based starting points. So, how do I tackle this problem then?
  
  Here's my current code:
  
  ```
  <div class="row">
      <div class="small-12 medium-12 large-12 columns">
          <div class="widget_event single_widget box_shadow">
              <h3>Hva skjer i username_0</h3>
  
              {% set startDateTime = now|date_modify('0:00')|date('c') %}
              {% set endDateTime = now|date_modify('first day of next month 4:00')|date('c') %}
  
              {% set startDateParam = 'and, >= ' ~ startDateTime ~ ', < ' ~ endDateTime %}
  
              {% set events = craft.entries.section('calendar').limit('15').startDate(startDateParam).order('startDate asc') %}
  
  {% for event in events %}
      <div class="event_list">
          <div class="single_event">
  
              <div class="date_left">
                  <p class="date">{{ event.startDate | date("d") }}.</p>
                  <p class="day">{{ event.startDate | date("M") }}</p>
              </div>
  
              <div class="title_middle">
                  <p class="title">{{ event.title }}</p>
                  <p class="desc">{{ event.body | hacksaw(words='75', allow='<b><strong><i><em>') }}</p>
              </div>
  
              <div class="time">{{ event.startDate | date("H:i") }}</div>
  
          </div>
      </div>
  {% endfor %}
  
  {% paginate craft.entries.section('calendar').limit(15) as pageEvents %}
      {% for month, entries in pageEvents | group("postDate | date('F')") %}
          <div class="event_nav text-center">
              <span class="left left_arrow">{% if paginate.prevUrl %}<a href="{{ paginate.prevUrl }}">Previous Month</a>{% endif %}</span>
              <span class="right right_arrow">{% if paginate.nextUrl %}<a href="{{ paginate.nextUrl }}">Next Month</a>{% endif %}</span>
          </div>
      {% endfor %}
  {% endpaginate %}
  
          </div>
      </div>
  </div>
  
  ```
  
  **Tags:** pagination
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-06-25. Score: 1 | Accepted answer
  
  Instead of using pagination you might want to look into setting up a [dynamic route](http://buildwithcraft.com/docs/routing#dynamic-routes) in the format "calendar/[year]/[month]". Adding 'year' and 'month' tags to your route will essentially pass those values as named variables to your template, which you can use to retrieve and display entries, as well as generate navigation to next/previous month's events. If you want the uri '/calendar' (without the 'year/month' uri segments) to link to the current months events you can test for the existence of the 'year' and 'month' variables, and if not defined, set the start month to the first day of the current month.
  
  ```
  {% if year is defined and month is defined %}
      {% set startMonth = year ~ '-' ~ month ~ '-' ~ 1 %}
  {% else %}
      {% set startMonth = now|date_modify("first day of this month") %}
  {% endif %}
  {% set nextMonth = startMonth|date_modify('+1 month')) %}
  {% set previousMonth = startMonth|date_modify('-1 month')) %}
  
  {% set entries = craft.entries.section('calendar').postDate('and', '>= ' ~ startMonth, '< ' ~ nextMonth %}
  
  {% for entry in entries %}
      {{ entry.title }}
      ...
  {% endfor %}
  
  <div class="pagination">
      <a href="{{ 'events/' ~ nextMonth|date("Y/n") }}">next month</a>
      <a href="{{ 'events/' ~ previousMonth|date("Y/n") }} ">previous month</a>
  </div>
  
  ```
  
  If required, within any month you could also use a pagination tag if you needed to limit the number of events displayed on any one page to 15.
  
  For additional examples see these answers, [here](https://craftcms.stackexchange.com/a/5456/784) and [here](https://craftcms.stackexchange.com/a/9527/784).
  
  > **Comments:**
  > **username_0**: Thanks for the example. I've never used Routes before. How would my actual route look like when the calendar in question is an include that is part of my homepage?  Would it be "calendar/year/month" ? Which template should I specify it to load then? Since it's an include, and not a template in the way I think of a template?  
  > **username_0**: Also, curious if it's possible to actually display the month name when paginating backward/forward? So instead of saying "Previous Month", it would say "May".  BTW: your current code works perfectly, except for the previous/next links, because of the Route part not being correct set up.  
  > **username_1**: Sorry, I think the month number without the leading zero is represented by 'n' instead of 'm'. I'll update the answer. See the [php date docs](http://php.net/manual/en/function.date.php) for more info. To output the month name, I believe you can use 'F': `<a href="{{ 'events/' ~ nextMonth|date("Y/n") }}">{{ nextMonth|date("F") }}</a>`  
  > **username_1**: Regarding the include question — if your looking to include events on the homepage, you might want to consider using ajax to load the calendar (to keep the uri's clean). Or perhaps consider having the "hompage" calendar (which shows the current month's events) link to the full calendar for previous/future events. Otherwise, if you want to incorporate routes with the homepage you would probably want to drop the 'calendar' segment of the route and just use `[year]/[month]` as your route, and specify the homepage template as the route template. The calendar could still be a template include.  
  
  ---
  
  