Querying Entries by Month and Year Good morning, If have have a URL that passes parameters: http://url?month=January&amp;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', '&gt;= ' ~ firstDayOfMonth, '&lt;= ' ~ lastDayOfMonth) %}
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', '&gt;= ' ~ (firstDayOfMonth|atom), '&lt;= ' ~ (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.