<p><strong>Solution 1:</strong></p>

<p>The problem with your code is, that you apply the <code>date</code> filter exclusively to the year variable:</p>

<blockquote>
  <p><code>{% set firstDayOfMonth = month ~ "/1/" ~ year|date("U") %}</code></p>
</blockquote>

<p>To get this right you have to first compose your "datetime string" and then apply the filter to the string as a whole:</p>

<pre><code>{% set firstDayOfMonth = (month ~ "/1/" ~ year)|date("U") %}
</code></pre>

<p>That's also the reason why it works for you to apply it inside the query.</p>

<p>.</p>

<p><strong>Solution 2:</strong></p>

<p>You don't need to pass a unix datetime formated string to your query, a string in format "Y-m-d" is just fine:</p>

<pre><code>{% set firstDayOfMonth = year ~ "-" ~ month ~ "-01" %}
{% set daysInThisMonth = firstDayOfMonth|date('t') %}

{% set lastDayOfMonth = year ~ "-" ~ month ~ "-" ~ daysInThisMonth %}

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

CC BY-SA 3.0
2014-06-24T20:54:30.247Z
2014-06-24T20:54:30.247Z
carlcs
<p>I think you can't use the unix time format. Try <code>|date('Y-m-d')</code> instead of <code>|date("U")</code>.</p>

<p>.</p>

<p><em>Edit:</em></p>

<p>You actually don't need to use the <code>|date</code> filter for that:</p>

<pre><code>{% set year = 2014 %}
{% set month = 06 %}

{% set firstDayOfMonth = year ~ "-" ~ month ~ "-01" %}
{% set daysInThisMonth = firstDayOfMonth|date('t') %}

{% set lastDayOfMonth = year ~ "-" ~ month ~ "-" ~ daysInThisMonth %} 

{{ firstDayOfMonth ~ " - " ~ lastDayOfMonth }}
</code></pre>

CC BY-SA 3.0
2014-06-24T20:55:10.493Z
@Clive I suppose you just replaced "/" with "-" and didn't try that code as a whole. It definitely works, see my other answer for more info! :)
carlcs
CC BY-SA 3.0
2014-06-18T10:54:21.480Z
This has the same effect - no results when I know there should be. It outputs the dates correctly, but treurns no entries. I think the problem is the way I'm using the dates?
Clive Portman
CC BY-SA 3.0
2014-06-18T11:06:11.957Z
@ClivePortman Can close your query with ) and add a .find() at the end?
Victor
CC BY-SA 3.0
2014-06-18T11:12:42.613Z
@Victor_In Have updated question so it includes the closing ). Adding .find() makes no difference.
Clive Portman
CC BY-SA 3.0
2014-06-18T09:15:01.840Z
2014-06-18T09:33:46.060Z
2014-06-18T09:33:46.060Z
carlcs
<p>Okay, here's the answer:</p>

<pre><code>{% set thisMonthsEvents = craft.entries.section('events').eventDate('and', '&gt;= ' ~ firstDayOfMonth | date("U"), '&lt;=' ~ lastDayOfMonth | date("U")) %}
</code></pre>

<p>Maybe this has something to do with me not formatting firstDayOfMonth and lastDayOfMonth as dates within the original query? Also, why doesn't this need a .find() after it?</p>

CC BY-SA 3.0
2014-06-18T11:36:10.537Z
This doesn't make sense to me. The only difference to the initial code is that you now apply the date filter inside the query or even twice?!
carlcs
CC BY-SA 3.0
2014-06-18T12:11:00.697Z
Adding .find() was just a guess, but soon we will know the answer: http://craftcms.stackexchange.com/q/265/177
Victor
CC BY-SA 3.0
2014-06-18T11:21:55.880Z
2014-06-18T11:29:52.383Z
2014-06-18T11:29:52.383Z
Clive Portman
<p>I've got a date field, and I'm grabbing the month and year from the url. What I'm after is to display all the events within the month, but for some reason I can't get this to work</p>

<pre><code>{% set firstDayOfMonth = month ~ "/1/" ~ year|date("U") %} // returns the 1st day of the month in unixtime

{% set daysInThisMonth = firstDayOfMonth|date('t') %}
{% set lastDayOfMonth = month ~ "/" ~ daysInThisMonth ~ "/" ~ year|date("U") %} // returns the last day of the month in unixtime

{% set thisMonthsEvents = craft.entries.section('events').eventDate('and', '&gt;= ' ~ firstDayOfMonth, '&lt;= ' ~ lastDayOfMonth) %}

{% if thisMonthsEvents | length %}
    some events
{% else %}
    no events
{% endif %}
</code></pre>

<p>Any ideas? firstDayOfMonth and lastDayOfMonth return dates fine, but no matter how I format them, the result is no entries returned. For example, firstDayOfTheMonth could output "2014-6-01" and lastDayOfMonth could output "2014-6-30", and I've got an event with an eventDate of 18th June 2014, but nothing is being returned.</p>

<datetime><date>
How to display entries with a custom date field within month
# How to display entries with a custom date field within month

(Asked by: username_0 on 2014-06-18)

I've got a date field, and I'm grabbing the month and year from the url. What I'm after is to display all the events within the month, but for some reason I can't get this to work
{% set firstDayOfMonth = month ~ "/1/" ~ year|date("U") %} // returns the 1st day of the month in unixtime

{% set daysInThisMonth = firstDayOfMonth|date('t') %}
{% set lastDayOfMonth = month ~ "/" ~ daysInThisMonth ~ "/" ~ year|date("U") %} // returns the last day of the month in unixtime

{% set thisMonthsEvents = craft.entries.section('events').eventDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}

{% if thisMonthsEvents | length %}
    some events
{% else %}
    no events
{% endif %}

Any ideas? firstDayOfMonth and lastDayOfMonth return dates fine, but no matter how I format them, the result is no entries returned. For example, firstDayOfTheMonth could output "2014-6-01" and lastDayOfMonth could output "2014-6-30", and I've got an event with an eventDate of 18th June 2014, but nothing is being returned.

**Tags:** datetime, date


---

## 3 answers

### Answer by username_1 on 2014-06-24. Score: 4 | Accepted answer

Solution 1:
The problem with your code is, that you apply the date filter exclusively to the year variable:

{% set firstDayOfMonth = month ~ "/1/" ~ year|date("U") %}

To get this right you have to first compose your "datetime string" and then apply the filter to the string as a whole:
{% set firstDayOfMonth = (month ~ "/1/" ~ year)|date("U") %}

That's also the reason why it works for you to apply it inside the query.
.
Solution 2:
You don't need to pass a unix datetime formated string to your query, a string in format "Y-m-d" is just fine:
{% set firstDayOfMonth = year ~ "-" ~ month ~ "-01" %}
{% set daysInThisMonth = firstDayOfMonth|date('t') %}

{% set lastDayOfMonth = year ~ "-" ~ month ~ "-" ~ daysInThisMonth %}

{% set thisMonthsEvents = craft.entries.section('events').eventDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}


---

### Answer by username_1 on 2014-06-18. Score: 2

I think you can't use the unix time format. Try |date('Y-m-d') instead of |date("U").
.
Edit:
You actually don't need to use the |date filter for that:
{% set year = 2014 %}
{% set month = 06 %}

{% set firstDayOfMonth = year ~ "-" ~ month ~ "-01" %}
{% set daysInThisMonth = firstDayOfMonth|date('t') %}

{% set lastDayOfMonth = year ~ "-" ~ month ~ "-" ~ daysInThisMonth %} 

{{ firstDayOfMonth ~ " - " ~ lastDayOfMonth }}

> **Comments:**
> **username_0**: This has the same effect - no results when I know there should be. It outputs the dates correctly, but treurns no entries. I think the problem is the way I'm using the dates?  
> **username_2**: @ClivePortman Can close your query with ) and add a .find() at the end?  
> **username_0**: @username_2_In Have updated question so it includes the closing ). Adding .find() makes no difference.  
> **username_1**: @Clive I suppose you just replaced "/" with "-" and didn't try that code as a whole. It definitely works, see my other answer for more info! :)  

---

### Answer by username_0 on 2014-06-18. Score: 2

Okay, here's the answer:
{% set thisMonthsEvents = craft.entries.section('events').eventDate('and', '>= ' ~ firstDayOfMonth | date("U"), '<=' ~ lastDayOfMonth | date("U")) %}

Maybe this has something to do with me not formatting firstDayOfMonth and lastDayOfMonth as dates within the original query? Also, why doesn't this need a .find() after it?

> **Comments:**
> **username_1**: This doesn't make sense to me. The only difference to the initial code is that you now apply the date filter inside the query or even twice?!  
> **username_2**: Adding .find() was just a guess, but soon we will know the answer: http://craftcms.stackexchange.com/q/265/177  

---

