2

I'm trying to pull entries from an events channel where a date field falls in the current month. Through google and other SE posts I've gotten to the following code:

{% set currMonth = now|date('m') %}
{% set currMonthFirst = currMonth ~ '/1/' ~ now|date('Y') %}
{% set nextMonthFirst = currMonthFirst|date_modify('+1 month') %}
<p>{{ currMonthFirst|date('m/d/Y') }} - {{ nextMonthFirst|date('m/d/Y') }}</p>

{% set events = craft.entries.section('events').event_startDateTime('>= ' ~ currMonthFirst|date('m/d/Y') ~ ', <' ~ nextMonthFirst|date('m/d/Y')) %}

{% for event in events %}
    <h1>{{event.title}}</h1>

{% endfor %}

currMonthFirst and nextMonthFirst both output the dates I'd expect, but when I use the set & for loop I get the following error - Fatal error: Call to a member function getTimestamp() on a non-object. Any idea what I'm doing wrong here or how to make what I'm trying to do work?

| improve this question | | | | |
4

This should get it sorted. There were 2 issues with yours. First is you were using the wrong 'and' syntax in the craft.entries call and second you need to use the Y-m-d pattern so MySQL understands to convert it a proper DateTime field instead of the m/d/Y format you had.

{% set currMonth = now|date('m') %}
{% set currMonthFirst = now|date('Y') ~ '-' ~ currMonth ~ '-1'  %}
{% set nextMonthFirst = currMonthFirst|date_modify('+1 month') %}

<p>{{ currMonthFirst|date('Y-m-d') }} - {{ nextMonthFirst|date('Y-m-d') }}</p>

{% set events = craft.entries.section('events').event_startDateTime('and', '>= ' ~ currMonthFirst|date('Y-m-d'), '< ' ~ nextMonthFirst|date('Y-m-d')) %}

{% for event in events %}
    <h1>{{event.title}}</h1>
{% endfor %}

Completely untested, but try using:

{% set events = craft.entries.section('events').event_startDateTime('and', '>= ' ~ currMonthFirst|date('m/d/Y'), '< ' ~ nextMonthFirst|date('m/d/Y')) %}
| improve this answer | | | | |
  • That doesn't fix the error, sadly. I've also tried casting both date variables into strings, but that doesn't fix the problem. Nor does trying the loop with just 'nextMonthFirst', which is a date variable to begin with ('currMonthFirst' ends up as a string). – Jacob Russell Feb 27 '15 at 21:13
  • Can you enable devMode and post the full stack trace to the error? – Brad Bell Feb 27 '15 at 21:14
  • Updated the answer. – Brad Bell Feb 27 '15 at 23:03
  • You are a wonderful human being. – Jacob Russell Feb 28 '15 at 18:09

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.