1

I have a Craft 3 site. For some reason the before filter isn't working when pulling entries from a channel, but after does. This is not a data issue.

{% set start = "2 months ago"|date() %}
{% set end = "next week"|date() %}

{% set listings = craft.entries({
    section: "tvListings",
    limit: null,
    with: "channel",
    before: end
}).all() %}

For clarity the dates result in:

  • Start: Feb 4, 2018
  • End: Apr 9, 2018

And one of the example entries is definitely in that date range (April 2nd).

enter image description here

If I ditch the "before" and go "after: start" it does work. If I try to say "give me all entries before Apr 9, 2018", the "Thunderbirds are go" (or any other listings) do not show.

Surely I'm not doing something super stupid right?

I'm running Craft 3.0.2.

Thanks.


EDIT: I've started pulling apart the CMS to debug. EntryQuery->beforePrepare() builds the query that filters by date. Although the props before and after are set, once they're passed through Db::parseDateParam() it mutilates the dates and somehow comes out with "2017-12-31 11:00:00" for both - so no bloody wonder it's not working. If this is a date format thing for those props I'll update the answer.

1 Answer 1

2

This is down to lacking documentation. Before/after are supposed to take "DateTime|string" as a parameter in the ElementQuery. The format is not documented.

Use "Y-m-d H:i:s"

{% set start = "2 months ago"|date("Y-m-d H:i:s") %}

Behind the scenes in building the query the CMS was replacing any dates I sent through with "2017-12-31 11:00:00" (no idea where this came from). After realising this I spelled out the before/after in format "Y-m-d H:i:s" and the dates started working.

Pixel and Tonic - Can you please update the docs to include the available date formats - or at least throw an error if it's not valid.

2
  • Noted @JamesNZ - Also worth mentioning the docs are in a public Github repo and pull requests are welcome. github.com/craftcms/docs
    – Brad Bell
    Commented Apr 4, 2018 at 22:31
  • Hey Brad. Oh of course - the only problem there is I don't know what the other valid date formats are. However if I can, I can I'll contribute back. Thanks.
    – JamesNZ
    Commented Apr 4, 2018 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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