Answers.Body: <p>The value you need to set the <code>postDate</code> parameter to is quite complex. If you define a dedicated variable for it, it makes the code more readable and you can more easily debug the value using the <code>dump()</code> function.</p>
  
  <p>For the problem itself, you were on the right track. The main problem is a syntax mistake, you are passing in the <code>'and', '&gt;= ', ...</code> values without wrapping them as an array.</p>
  
  <p>Some other changes I’d suggest is to use the <code>&lt;</code> operator in combination with a relative time of <code>'first day of next month'</code>, so every entry before 00:00 of that day (see documentation of <a href="http://php.net/manual/en/datetime.formats.relative.php" rel="noreferrer">relative date/time formats</a>).</p>
  
  <p>And instead of the <code>.date</code> property, I'd rather use the <code>date</code> filter to output the DateTime object as a string in the right format. Which can be <code>'Y-m-d'</code> to omit the time completely (Craft sets it back to 00:00), or one of the characters that output a full date with time <code>'c'</code> or <code>'U'</code> (see documentation of <a href="http://php.net/manual/en/function.date.php" rel="noreferrer">date format parameter</a>).</p>
  
  <pre><code>{% set firstDay = date(year ~ '-' ~ month ~ '-01') %}
  {% set lastDay = firstDay|date_modify('first day of next month') %}
  
  {% set postDateParam = ['and', '&gt;= ' ~ firstDay|date('c'), '&lt; ' ~ lastDay|date('c')] %}
  
  &lt;pre&gt;{{ dump(postDateParam) }}&lt;/pre&gt;
  
  {% set archive = craft.entries({
      section: 'blog',
      postDate: postDateParam,
  }) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-05T13:45:05.887Z
Answers.Text: This works and was easy to understand, thank you so much!
Answers.UserDisplayName: Maria
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-05T15:18:11.023Z
Answers.Text: I had the same doubt.
  Thank you very much.
Answers.UserDisplayName: Paulo Vitor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-05T13:26:08.027Z
Answers.LastActivityDate: 2018-04-24T07:52:33.547Z
Answers.LastEditDate: 2018-04-24T07:52:33.547Z
Answers.UserDisplayName: carlcs
Body: <p>I'm trying to make a archive page for a blog sorted by months. I tried to consult the solution from <a href="https://craftcms.stackexchange.com/questions/11508/linking-and-showing-archive-by-month">Linking and showing archive by month</a>, but I can't get it to work like it should. The code I have now just displays all entries regardless of their post date.</p>
  
  <pre><code>{% set catch = year ~ '-' ~ month ~ '-01' %}
  {% set firstDay = catch | date_modify('first day of ', month ~ year ~ ' 00:00:00') %}
  {% set lastDay = catch | date_modify('last day of ', month ~ year ~ ' 23:59:59') %}
  {% set archive  = craft.entries.section('blog').postDate('and', '&gt;= ' ~ firstDay.date, '&lt;= ' ~ lastDay.date) %}
  {% for entry in archive %}
      &lt;div class="posts-block"&gt;
          &lt;h3&gt;{{ entry.title }}&lt;/h3&gt;
      &lt;/div&gt;
  {% endfor %}
  </code></pre>
  
  <p>I had to add ".date" behind "firstDay" and "lastDay", or I get an error (Object of class DateTime could not be converted to string) If I remove the "and" from "archive" like this:</p>
  
  <pre><code>{% set archive  = craft.entries.section('blog').postDate('&gt;= ' ~ firstDay.date, '&lt;= ' ~ lastDay.date) %}
  </code></pre>
  
  <p>then it sorts my entries according to "firstDay", but completely ignores "lastDay". I tried to threw an and into the middle of the string, but that didn't to anything.</p>
  
  <p>I also tried to use .after and .before, but then I just get what seems like a random set of entries...</p>
  
  <pre><code>{% set archive = craft.entries.section('blog').limit(null).after(firstDay).before(lastDay) %}
  </code></pre>
  
Tags: <entries><postdate><archives>
Title: Filter entries by month
ThreadText: # Filter entries by month
  
  (Asked by: username_0 on 2017-10-05)
  
  I'm trying to make a archive page for a blog sorted by months. I tried to consult the solution from [Linking and showing archive by month](https://craftcms.stackexchange.com/questions/11508/linking-and-showing-archive-by-month), but I can't get it to work like it should. The code I have now just displays all entries regardless of their post date.
  
  ```
  {% set catch = year ~ '-' ~ month ~ '-01' %}
  {% set firstDay = catch | date_modify('first day of ', month ~ year ~ ' 00:00:00') %}
  {% set lastDay = catch | date_modify('last day of ', month ~ year ~ ' 23:59:59') %}
  {% set archive  = craft.entries.section('blog').postDate('and', '>= ' ~ firstDay.date, '<= ' ~ lastDay.date) %}
  {% for entry in archive %}
      <div class="posts-block">
          <h3>{{ entry.title }}</h3>
      </div>
  {% endfor %}
  
  ```
  
  I had to add ".date" behind "firstDay" and "lastDay", or I get an error (Object of class DateTime could not be converted to string) If I remove the "and" from "archive" like this:
  
  ```
  {% set archive  = craft.entries.section('blog').postDate('>= ' ~ firstDay.date, '<= ' ~ lastDay.date) %}
  
  ```
  
  then it sorts my entries according to "firstDay", but completely ignores "lastDay". I tried to threw an and into the middle of the string, but that didn't to anything.
  
  I also tried to use .after and .before, but then I just get what seems like a random set of entries...
  
  ```
  {% set archive = craft.entries.section('blog').limit(null).after(firstDay).before(lastDay) %}
  
  ```
  
  **Tags:** entries, postdate, archives
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-10-05. Score: 8 | Accepted answer
  
  The value you need to set the `postDate` parameter to is quite complex. If you define a dedicated variable for it, it makes the code more readable and you can more easily debug the value using the `dump()` function.
  
  For the problem itself, you were on the right track. The main problem is a syntax mistake, you are passing in the `'and', '>= ', ...` values without wrapping them as an array.
  
  Some other changes I’d suggest is to use the `<` operator in combination with a relative time of `'first day of next month'`, so every entry before 00:00 of that day (see documentation of [relative date/time formats](http://php.net/manual/en/datetime.formats.relative.php)).
  
  And instead of the `.date` property, I'd rather use the `date` filter to output the DateTime object as a string in the right format. Which can be `'Y-m-d'` to omit the time completely (Craft sets it back to 00:00), or one of the characters that output a full date with time `'c'` or `'U'` (see documentation of [date format parameter](http://php.net/manual/en/function.date.php)).
  
  ```
  {% set firstDay = date(year ~ '-' ~ month ~ '-01') %}
  {% set lastDay = firstDay|date_modify('first day of next month') %}
  
  {% set postDateParam = ['and', '>= ' ~ firstDay|date('c'), '< ' ~ lastDay|date('c')] %}
  
  <pre>{{ dump(postDateParam) }}</pre>
  
  {% set archive = craft.entries({
      section: 'blog',
      postDate: postDateParam,
  }) %}
  
  ```
  
  > **Comments:**
  > **username_0**: This works and was easy to understand, thank you so much!  
  > **username_2**: I had the same doubt. Thank you very much.  
  
  ---
  
  