1

I am trying to get all entries from my events section in Craft 3 that occur after 17:00.

My current code is: {% set events = craft.entries.section('events').eventStartTime('>= ' ~ '17:00'|time ).all() %}

The >= is being ignored as far as I can tell. The query just returns all events.

The eventStartTime field is a Date/Time field that is set to just show the time (I know that behind the scenes Craft still treats this as a DateTime object but I was hoping that the |time filter would help with that.

When I have created similar queries on a DateTime field but looking at the date rather than time, they have worked as expected - maybe I am missing something when it comes to the time portion of the object?

Any help would be appreciated.

  • Is this Craft 2 or 3? – Brad Bell Mar 28 at 3:29
  • Ah sorry, Craft 3 – jaypratt Mar 28 at 8:15
1

That will give you all entries, from today (so not past entries) starting after 5PM:

{% set startAfterTime = '17.00'|date('U') %}
{% set today = now|atom|date %}

{% set events = craft.entries()
    .section('events')
    .status(['live', 'pending'])
    .postDate('>=' ~ today )
    .eventStartTime('>=' ~ startAfterTime)
    .all() %}
  • This is pretty much identical to the approach I tried first and unfortunately does not work, I just have all entries returned to me. – jaypratt Apr 1 at 7:38
  • @jaypratt that's really strange, I tested it here as well? Is it just a standard entry Section..? – Oli Apr 1 at 12:03
  • Yep - just a channel. – jaypratt Apr 2 at 14:19
1

For me it worked after I changed the date format into {% set today = now|date('Y-m-d') %}, so I think this should work:

{% set startAfterTime = '17.00'|date('U') %}
{% set today = now|date('Y-m-d') %}

{% set events = craft.entries()
    .section('events')
    .status(['live', 'pending'])
    .postDate('>=' ~ today )
    .eventStartTime('>=' ~ startAfterTime)
    .all() %}

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.