Answers.Body: <p>As mentioned in the comments, using a <a href="http://buildwithcraft.com/docs/routing#dynamic-routes" rel="nofollow noreferrer">dynamic route</a> is good method to create a date based uri (i.e. 'calendar/[year]-[month]'). See <a href="https://craftcms.stackexchange.com/a/10353/784">this answer</a> for an example.</p>
  
  <p>Adding 'category' into the route is a little more difficult, since there is no 'category' tag built into the dynamic route settings. However, you can use the wildcard <code>*</code> as part of the route for what would be the category (or anything else for that matter) — and then retrieve it using <code>craft.request.segment</code> method.</p>
  
  <p><strong>Dynamic route</strong> (which points to your 'calendar' template)</p>
  
  <pre><code>calendar/[*]/[year]-[month]
  </code></pre>
  
  <p>(note: using <code>calendar/[slug]/[year]-[month]</code> might also resolve to a category entry — don't know, but worth a try.)</p>
  
  <p><strong>Calendar template</strong></p>
  
  <pre><code>{% set categorySlug = craft.request.segment(2) %}
  {% set category = craft.category.slug(categorySlug).first %}
  {% 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).relatedTo(category) %}
  
  {% for entry in entries %}
      {{ entry.title }}
      ...
  {% endfor %}
  
  &lt;div class="pagination"&gt;
      &lt;a href="{{ 'events/' ~ category ~ '/' ~ nextMonth|date("Y-n") }}"&gt;next month&lt;/a&gt;
      &lt;a href="{{ 'events/' ~ category ~ '/' ~ previousMonth|date("Y-n") }} "&gt;previous month&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Advanced route method</strong></p>
  
  <p>If you want to be more specific in your route, then you can also set up an <a href="http://buildwithcraft.com/docs/routing#advanced-routing" rel="nofollow noreferrer">advanced route</a> using named sub-patterns, which you can add to <code>craft/config/routes.php</code>, that will provide named variables to your template for 'category', 'year' and 'month'. Something like,</p>
  
  <pre><code>'calendar/(?P&lt;category&gt;\w+)/(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;\d{2})' =&gt; 'calendar/_index',
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-29T17:25:37.883Z
Answers.LastActivityDate: 2015-06-29T17:36:49.867Z
Answers.LastEditDate: 2017-04-13T12:50:59.930Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I have a template, <em>/calendar</em>, which outputs a list of all entries from the <em>events</em> channel. If a user clicks a link for "July 2015", I want the same template to load showing only the events for July 2015. I can fetch the entries for a given month with something like this.</p>
  
  <pre><code>{% set setMonth = '2015-07' %}
  
  {% set firstDay, lastDay
     =
     setMonth ~ '-01', 
     setMonth ~ '-' ~ setMonth|date('t')
  %}
  {% set after, before
     =
     firstDay|date_modify('-1 day')|date('Y-m'), 
     lastDay|date_modify('+1 day')|date('Y-m')
  %}
  
  {% set events = craft.entries.section('events').after(after).before(before)
  %}
  </code></pre>
  
  <p>I want to pass the date that will be assigned to <code>setMonth</code> in the URL using something like <em>/calendar/2015-07</em>, but I currently get a 404 error.</p>
  
  <p>How can I pass the date in the URL without throwing a 404?</p>
  
  <p>To make it even more complex, this template can also be filtered by category, so there would also be times the URL would look something like <em>/calendar/catslug/2015-07</em>. In this case, the template should display only the July events from the catslug category.</p>
  
  <p>Assuming I can conquer the aforementioned 404 problem, will Craft still recognize <em>/calendar/catslug/2015-07</em> as a category-triggering URL, or will the addition of the date segment break that functionality?</p>
  
  <p>I suspect this isn't as difficult as I'm making it, so any assistance is appreciated.</p>
  
  <p>Thanks in advance.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-27T17:03:51.320Z
Comments.Text: [This answer](http://craftcms.stackexchange.com/a/10353/784) might answer your question. If not, leave a comment, and I can try and clarify.
Comments.UserDisplayName: Douglas McDonald
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-28T06:18:11.167Z
Comments.Text: I agree with @DouglasMcDonald: you should look into routes for this.
Comments.UserDisplayName: Marion Newlevant
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-29T14:22:41.683Z
Comments.Text: I have the date filter working with routes now, so thanks for that pointer. For the second part of my question, is there any way to do this without breaking category functionality. `/calendar/catslug/2015-07` causes Craft to no longer recognize `catslug` as a category indicator, so any category-based filtering is lost.
Comments.UserDisplayName: Philip Jones
Tags: <entries><url><filter><postdate>
Title: Setting Date from URL
ThreadText: # Setting Date from URL
  
  (Asked by: username_0 on 2015-06-26)
  
  I have a template, */calendar*, which outputs a list of all entries from the *events* channel. If a user clicks a link for "July 2015", I want the same template to load showing only the events for July 2015. I can fetch the entries for a given month with something like this.
  
  ```
  {% set setMonth = '2015-07' %}
  
  {% set firstDay, lastDay
     =
     setMonth ~ '-01', 
     setMonth ~ '-' ~ setMonth|date('t')
  %}
  {% set after, before
     =
     firstDay|date_modify('-1 day')|date('Y-m'), 
     lastDay|date_modify('+1 day')|date('Y-m')
  %}
  
  {% set events = craft.entries.section('events').after(after).before(before)
  %}
  
  ```
  
  I want to pass the date that will be assigned to `setMonth` in the URL using something like */calendar/2015-07*, but I currently get a 404 error.
  
  How can I pass the date in the URL without throwing a 404?
  
  To make it even more complex, this template can also be filtered by category, so there would also be times the URL would look something like */calendar/catslug/2015-07*. In this case, the template should display only the July events from the catslug category.
  
  Assuming I can conquer the aforementioned 404 problem, will Craft still recognize */calendar/catslug/2015-07* as a category-triggering URL, or will the addition of the date segment break that functionality?
  
  I suspect this isn't as difficult as I'm making it, so any assistance is appreciated.
  
  Thanks in advance.
  
  **Tags:** entries, url, filter, postdate
  
  > **Comments:**
  > **username_1**: [This answer](http://craftcms.stackexchange.com/a/10353/784) might answer your question. If not, leave a comment, and I can try and clarify.  
  > **username_2**: I agree with @username_1: you should look into routes for this.  
  > **username_0**: I have the date filter working with routes now, so thanks for that pointer. For the second part of my question, is there any way to do this without breaking category functionality. `/calendar/catslug/2015-07` causes Craft to no longer recognize `catslug` as a category indicator, so any category-based filtering is lost.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-06-29. Score: 2
  
  As mentioned in the comments, using a [dynamic route](http://buildwithcraft.com/docs/routing#dynamic-routes) is good method to create a date based uri (i.e. 'calendar/[year]-[month]'). See [this answer](https://craftcms.stackexchange.com/a/10353/784) for an example.
  
  Adding 'category' into the route is a little more difficult, since there is no 'category' tag built into the dynamic route settings. However, you can use the wildcard `*` as part of the route for what would be the category (or anything else for that matter) — and then retrieve it using `craft.request.segment` method.
  
  **Dynamic route** (which points to your 'calendar' template)
  
  ```
  calendar/[*]/[year]-[month]
  
  ```
  
  (note: using `calendar/[slug]/[year]-[month]` might also resolve to a category entry — don't know, but worth a try.)
  
  **Calendar template**
  
  ```
  {% set categorySlug = craft.request.segment(2) %}
  {% set category = craft.category.slug(categorySlug).first %}
  {% 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).relatedTo(category) %}
  
  {% for entry in entries %}
      {{ entry.title }}
      ...
  {% endfor %}
  
  <div class="pagination">
      <a href="{{ 'events/' ~ category ~ '/' ~ nextMonth|date("Y-n") }}">next month</a>
      <a href="{{ 'events/' ~ category ~ '/' ~ previousMonth|date("Y-n") }} ">previous month</a>
  </div>
  
  ```
  
  **Advanced route method**
  
  If you want to be more specific in your route, then you can also set up an [advanced route](http://buildwithcraft.com/docs/routing#advanced-routing) using named sub-patterns, which you can add to `craft/config/routes.php`, that will provide named variables to your template for 'category', 'year' and 'month'. Something like,
  
  ```
  'calendar/(?P<category>\w+)/(?P<year>\d{4})/(?P<month>\d{2})' => 'calendar/_index',
  
  ```
  
  
  ---
  
  