<p>You can pull query string variables using the <a href="https://buildwithcraft.com/classreference/services/HttpRequestService" rel="nofollow">HttpRequestService</a>, like this:</p>

<pre><code>{% set month = craft.request.query('month') %}
{% set year = craft.request.query('year') %}
</code></pre>

<p>Then, you'll need to construct a DateTime object from those query parameters:</p>

<pre><code>{% set queryDate = date([month, year]|join(' ')) %}
</code></pre>

<p>...and finally, include that date object in the criteria for <code>craft.entries</code> (notice the <code>&gt;=</code> operator, which means we should get entries with a <code>availabilityStartDate</code> value equal or higher to the <code>queryDate</code> date object:</p>

<pre><code>{% set allEntries = craft.entries.section('availability').order('availabilityStartDate').availabilityStartDate('&gt;= ' ~ queryDate.date).limit(null) %}
</code></pre>

<p><strong>EDIT</strong></p>

<p>Filtering out entries with date field values matching the queried month and year <em>exactly</em> is a bit more complicated.</p>

<p>There are probably several ways to approach this, but a thing that works is to use both a start date (i.e. the first day of the queried month) as well as an end date (i.e. the last day of the queried month) for the criteria. </p>

<p>Being that the <code>queryDate</code> variable actually already refers to the first day of the queried month, we'll just need to add 1 month for the end date:</p>

<pre><code>{% set queryStartDate = date([month, year]|join(' ')) %}
{% set queryEndDate = queryStartDate.date|date_modify('+1 month') %}
</code></pre>

<p>And here's the element criteria with both date object statements:</p>

<pre><code>{% set allEntries = craft.entries.section('availability ').order('availabilityStartDate').availabilityStartDate('and', '&gt;= ' ~ queryStartDate.date, '&lt; ' ~ queryEndDate.date).limit(null) %}
</code></pre>

<p>Here's everything together, for cohesion:</p>

<pre><code>{# Get month and year query parameters #}
{% set month = craft.request.query('month') %}
{% set year = craft.request.query('year') %}

{# Create DateTime objects for the start date and end date (+1 month) #}
{% set queryStartDate = date([month, year]|join(' ')) %}
{% set queryEndDate = queryStartDate.date|date_modify('+1 month') %}

{# Get the entries #}
{% set allEntries = craft.entries.section('availability ').order('availabilityStartDate').availabilityStartDate('and', '&gt;= ' ~ queryStartDate.date, '&lt; ' ~ queryEndDate.date).limit(null) %}
</code></pre>

<p>PS: When you have multiple parameters to <code>craft.entries</code> like this, you might want to consider the object syntax for readability:</p>

<pre><code>{% set allEntries = craft.entries({
    section : 'availability',
    order : 'availabilityStartDate',
    availabilityStartDate : 'and, &gt;= ' ~ queryStartDate.date ~ ', &lt; ' ~ queryEndDate.date,
    limit : null
}) %}
</code></pre>

CC BY-SA 3.0
2015-11-12T17:44:44.480Z
Hi mmikkel, thanks for taking the time to answer me - much appreciated. I've used {% for entry in allEntries %} {{ entry.availabilityStartDate | date ("j F Y") }}<br>{% endfor %} to output the entries that should have month=november and year=2015.The output includes all entries in december 2015, but is also including january 2016, february 2016 - whereas I only want entries where month=november and year=2015. Got any ideas, I've tried several things to no avail.
user1070143
CC BY-SA 3.0
2015-11-12T18:26:18.017Z
Aha, I didn't catch that detail, sorry. You'll want to use the `=` operator over `>=`, but you'll also need to normalize the dates. I'm in a rush right now but I'll look at it later if you don't get another answer (or an edit) :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-11-13T11:17:17.973Z
Hi mmikkel, when you've got a moment help with normalizing the dates would be great. Have a good weekend!
user1070143
CC BY-SA 3.0
2015-11-13T18:24:09.797Z
Hello @user1070143 – check out the edit, hopefully this will work out for you. A good weekend to you to!
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-11-18T20:54:16.953Z
Hi mmikkel, thank you very much for the time and expertise you put into answering my questions. If you care to drop me your paypal email I'd like to buy you a bottle of wine.
user1070143
CC BY-SA 3.0
2015-11-19T09:26:24.087Z
@user1070143 Haha, thanks – the offer is much appreciated but totally not necessary :) You can buy me a beer if we ever meet at a Craft conference ;)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-11-19T16:18:53.893Z
Someone said beer/wine?
Brad Bell
CC BY-SA 3.0
2015-11-19T16:26:11.353Z
Yes. And "Craft conference" :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-11-12T14:47:12.063Z
2015-11-13T18:40:39.707Z
2015-11-13T18:40:39.707Z
Mats Mikkel Rummelhoff
<p>How do I filter entries by URL parameters. The url is <code>/booking?month=november&amp;year=2015</code>.</p>

<p>So far I have:</p>

<pre><code>{% set allEntries = craft.entries.section('availability').order('availabilityStartDate').limit(null) %}
</code></pre>

<p>where <code>availabilityStartDate</code> is a date field.</p>

<p>How do I return entries that match November and 2015.</p>

<p>Thanks for any ideas or code!</p>

<templating><datetime><filter>
How do I use URL parameters to filter a channel by a date field?
# How do I use URL parameters to filter a channel by a date field?

(Asked by: username_0 on 2015-11-12)

How do I filter entries by URL parameters. The url is /booking?month=november&year=2015.
So far I have:
{% set allEntries = craft.entries.section('availability').order('availabilityStartDate').limit(null) %}

where availabilityStartDate is a date field.
How do I return entries that match November and 2015.
Thanks for any ideas or code!

**Tags:** templating, datetime, filter

---

## 1 answers

### Answer by username_1 on 2015-11-12. Score: 5 | Accepted answer

You can pull query string variables using the HttpRequestService, like this:
{% set month = craft.request.query('month') %}
{% set year = craft.request.query('year') %}

Then, you'll need to construct a DateTime object from those query parameters:
{% set queryDate = date([month, year]|join(' ')) %}

...and finally, include that date object in the criteria for craft.entries (notice the >= operator, which means we should get entries with a availabilityStartDate value equal or higher to the queryDate date object:
{% set allEntries = craft.entries.section('availability').order('availabilityStartDate').availabilityStartDate('>= ' ~ queryDate.date).limit(null) %}

EDIT
Filtering out entries with date field values matching the queried month and year exactly is a bit more complicated.
There are probably several ways to approach this, but a thing that works is to use both a start date (i.e. the first day of the queried month) as well as an end date (i.e. the last day of the queried month) for the criteria. 
Being that the queryDate variable actually already refers to the first day of the queried month, we'll just need to add 1 month for the end date:
{% set queryStartDate = date([month, year]|join(' ')) %}
{% set queryEndDate = queryStartDate.date|date_modify('+1 month') %}

And here's the element criteria with both date object statements:
{% set allEntries = craft.entries.section('availability ').order('availabilityStartDate').availabilityStartDate('and', '>= ' ~ queryStartDate.date, '< ' ~ queryEndDate.date).limit(null) %}

Here's everything together, for cohesion:
{# Get month and year query parameters #}
{% set month = craft.request.query('month') %}
{% set year = craft.request.query('year') %}

{# Create DateTime objects for the start date and end date (+1 month) #}
{% set queryStartDate = date([month, year]|join(' ')) %}
{% set queryEndDate = queryStartDate.date|date_modify('+1 month') %}

{# Get the entries #}
{% set allEntries = craft.entries.section('availability ').order('availabilityStartDate').availabilityStartDate('and', '>= ' ~ queryStartDate.date, '< ' ~ queryEndDate.date).limit(null) %}

PS: When you have multiple parameters to craft.entries like this, you might want to consider the object syntax for readability:
{% set allEntries = craft.entries({
    section : 'availability',
    order : 'availabilityStartDate',
    availabilityStartDate : 'and, >= ' ~ queryStartDate.date ~ ', < ' ~ queryEndDate.date,
    limit : null
}) %}

> **Comments:**
> **username_0**: Hi mmikkel, thanks for taking the time to answer me - much appreciated. I've used {% for entry in allEntries %} {{ entry.availabilityStartDate | date ("j F Y") }}<br>{% endfor %} to output the entries that should have month=november and year=2015.The output includes all entries in december 2015, but is also including january 2016, february 2016 - whereas I only want entries where month=november and year=2015. Got any ideas, I've tried several things to no avail.  
> **username_1**: Aha, I didn't catch that detail, sorry. You'll want to use the `=` operator over `>=`, but you'll also need to normalize the dates. I'm in a rush right now but I'll look at it later if you don't get another answer (or an edit) :)  
> **username_0**: Hi mmikkel, when you've got a moment help with normalizing the dates would be great. Have a good weekend!  
> **username_1**: Hello @user1070143 – check out the edit, hopefully this will work out for you. A good weekend to you to!  
> **username_0**: Hi mmikkel, thank you very much for the time and expertise you put into answering my questions. If you care to drop me your paypal email I'd like to buy you a bottle of wine.  
> **username_1**: @user1070143 Haha, thanks – the offer is much appreciated but totally not necessary :) You can buy me a beer if we ever meet at a Craft conference ;)  
> **username_2**: Someone said beer/wine?  
> **username_1**: Yes. And "Craft conference" :)  

---

