<p>Jon's answer is excellent, but if you want to keep the same <code>month</code> and <code>year</code> URL query params, I offer this.</p>

<p>The problem is that <code>firstDayOfMonth</code> and <code>lastDayOfMonth</code> are PHP class objects and can't be inlined as text. They need to be formatted as text with a <code>date()</code> or <code>atom</code> filter.</p>

<p>Here's your code with the filters added:</p>

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

{% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set lastDayOfMonth = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

{% set entries = craft.entries.section('yourSection').postDate('and', '&gt;= ' ~ (firstDayOfMonth|atom), '&lt;= ' ~ (lastDayOfMonth|atom)) %}
</code></pre>

<p>You can make it extra readable by using the <a href="https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#after" rel="nofollow noreferrer"><code>after</code></a> and <a href="https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#before" rel="nofollow noreferrer"><code>before</code></a> methods since your query is based on Craft's <code>postDate</code> parameter.</p>

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

{% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set firstDayOfNextMonth = firstDayOfMonth | date_modify('+1 month') %}

{% set entries = craft.entries
    .section('yourSection')
    .after(firstDayOfMonth|atom)
    .before(firstDayOfNextMonth|atom)
    .all()
%}
</code></pre>

<p>I'm sure you're aware, but it's worth noting for anyone finding this answer later: This is a bit fragile. If the URL query string does not have a valid month name and four-digit year number, it'll chuck errors.</p>

CC BY-SA 4.0
2019-02-08T16:15:59.920Z
2019-02-08T16:15:59.920Z
Sam Hernandez
<p>You'll want to use the dateCreated entry query: <a href="https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#datecreated" rel="nofollow noreferrer">https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#datecreated</a>. You'll also want to change the parameter to something like <code>?startdate=2018-01-01&amp;enddate=2018-01-31</code>.</p>

<pre><code>{# create variables for start and end dates, getting values from params #}
{% set start = craft.app.request.getParam(startdate)|atom %}
{% set end = craft.app.request.getParam(enddate)|atom %}

{# loop through entries that match date created, and set loop to a variable #}
{% set entries = craft.entries()
    .dateCreated(['and', "&gt;= #{start}", "&lt; #{end}"])
    .all() %}
</code></pre>

CC BY-SA 4.0
2019-02-08T15:14:28.680Z
2019-02-08T15:14:28.680Z
Jon Weedin
<p>I also found a solution. Seems to be working correctly. If I can simplify it, please let me know.</p>

<pre><code>            {% set month = craft.app.request.getParam('month') ?? null %}
            {% set year = craft.app.request.getParam('year') ?? null %}

            {% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
            {% set lastDayOfMonth = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

            {% set firstDayOfMonth = firstDayOfMonth|date('Y-m-d') %}
            {% set lastDayOfMonth = lastDayOfMonth|date('Y-m-d') %}

            {% if month|length %}
                {# {% set newsEntries = craft.entries.section('news').postDate('and', '&gt;= ' ~ firstDayOfMonth|date('Y-m-d'), '&lt;= ' ~ lastDayOfMonth|date('Y-m-d')) %} #}
                {% paginate craft.entries.section('news').postDate(['and', "&gt;= #{firstDayOfMonth}", "&lt;= #{lastDayOfMonth}"]).orderBy('postDate desc').limit(10) as newsInfo, newsEntries %}
                &lt;h3&gt;{{ month}} {{ year }} Archives&lt;/h3&gt;
            {% else %}
                {% paginate craft.entries.section('news').orderBy('postDate desc').limit(10) as newsInfo, newsEntries %}
                &lt;h3&gt;All Releases&lt;/h3&gt;
            {% endif %}
</code></pre>

CC BY-SA 4.0
2019-02-08T16:34:29.233Z
2019-02-08T16:34:29.233Z
Jason
<p>Good morning, If have have a URL that passes parameters: <a href="http://url?month=January&amp;Year=2018" rel="nofollow noreferrer">http://url?month=January&amp;Year=2018</a>, how would I go about querying all entries in section where the postDate equals January 2018?</p>

<p>I can get the parameters by using: <code>craft.app.request.getParam('month')</code> and <code>craft.app.request.getParam('year')</code> but I can seem to figure out the correct way to only show me entries based on these parameters. </p>

<p>Thanks in advance.</p>

<hr>

<p>I noticed a post that had the following answer and the response was that it worked. I tried it, and received the error:
<code>Object of class DateTime could not be converted to string</code>.</p>

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

{% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set lastDayOfMonth = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

{% set entries = craft.entries.section('yourSection').postDate('and', '&gt;= ' ~ firstDayOfMonth, '&lt;= ' ~ lastDayOfMonth) %}
</code></pre>

<craft3>
Querying Entries by Month and Year
# Querying Entries by Month and Year

(Asked by: username_0 on 2019-02-08)

Good morning, If have have a URL that passes parameters: http://url?month=January&Year=2018, how would I go about querying all entries in section where the postDate equals January 2018?
I can get the parameters by using: craft.app.request.getParam('month') and craft.app.request.getParam('year') but I can seem to figure out the correct way to only show me entries based on these parameters. 
Thanks in advance.

I noticed a post that had the following answer and the response was that it worked. I tried it, and received the error:
Object of class DateTime could not be converted to string.
{% set month = craft.request.getParam('month') %}
{% set year = craft.request.getParam('year')  %}

{% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set lastDayOfMonth = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

{% set entries = craft.entries.section('yourSection').postDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}

**Tags:** craft3


---

## 3 answers

### Answer by username_1 on 2019-02-08. Score: 3 | Accepted answer

Jon's answer is excellent, but if you want to keep the same month and year URL query params, I offer this.
The problem is that firstDayOfMonth and lastDayOfMonth are PHP class objects and can't be inlined as text. They need to be formatted as text with a date() or atom filter.
Here's your code with the filters added:
{% set month = craft.request.getParam('month') %}
{% set year = craft.request.getParam('year')  %}

{% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set lastDayOfMonth = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

{% set entries = craft.entries.section('yourSection').postDate('and', '>= ' ~ (firstDayOfMonth|atom), '<= ' ~ (lastDayOfMonth|atom)) %}

You can make it extra readable by using the after and before methods since your query is based on Craft's postDate parameter.
{% set month = craft.request.getParam('month') %}
{% set year = craft.request.getParam('year')  %}

{% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set firstDayOfNextMonth = firstDayOfMonth | date_modify('+1 month') %}

{% set entries = craft.entries
    .section('yourSection')
    .after(firstDayOfMonth|atom)
    .before(firstDayOfNextMonth|atom)
    .all()
%}

I'm sure you're aware, but it's worth noting for anyone finding this answer later: This is a bit fragile. If the URL query string does not have a valid month name and four-digit year number, it'll chuck errors.


---

### Answer by username_2 on 2019-02-08. Score: 2

You'll want to use the dateCreated entry query: https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#datecreated. You'll also want to change the parameter to something like ?startdate=2018-01-01&enddate=2018-01-31.
{# create variables for start and end dates, getting values from params #}
{% set start = craft.app.request.getParam(startdate)|atom %}
{% set end = craft.app.request.getParam(enddate)|atom %}

{# loop through entries that match date created, and set loop to a variable #}
{% set entries = craft.entries()
    .dateCreated(['and', ">= #{start}", "< #{end}"])
    .all() %}


---

### Answer by username_0 on 2019-02-08. Score: 1

I also found a solution. Seems to be working correctly. If I can simplify it, please let me know.
            {% set month = craft.app.request.getParam('month') ?? null %}
            {% set year = craft.app.request.getParam('year') ?? null %}

            {% set firstDayOfMonth = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
            {% set lastDayOfMonth = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

            {% set firstDayOfMonth = firstDayOfMonth|date('Y-m-d') %}
            {% set lastDayOfMonth = lastDayOfMonth|date('Y-m-d') %}

            {% if month|length %}
                {# {% set newsEntries = craft.entries.section('news').postDate('and', '>= ' ~ firstDayOfMonth|date('Y-m-d'), '<= ' ~ lastDayOfMonth|date('Y-m-d')) %} #}
                {% paginate craft.entries.section('news').postDate(['and', ">= #{firstDayOfMonth}", "<= #{lastDayOfMonth}"]).orderBy('postDate desc').limit(10) as newsInfo, newsEntries %}
                {{ month}} {{ year }} Archives
            {% else %}
                {% paginate craft.entries.section('news').orderBy('postDate desc').limit(10) as newsInfo, newsEntries %}
                All Releases
            {% endif %}


---

