3

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.

4

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) %}
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 }}
  • 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 Jun 18 '14 at 10:54
  • @ClivePortman Can close your query with ) and add a .find() at the end? – Victor In Jun 18 '14 at 11:06
  • @Victor_In Have updated question so it includes the closing ). Adding .find() makes no difference. – Clive Portman Jun 18 '14 at 11:12
  • @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 Jun 24 '14 at 20:55
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?

  • 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 Jun 18 '14 at 11:36
  • Adding .find() was just a guess, but soon we will know the answer: craftcms.stackexchange.com/q/265/177 – Victor In Jun 18 '14 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.