1

I've got a date string from the url in this format: "2021-06-01" (year-month-day) and I want to find entries that have a date in the year and month from that string. The date is a date field in Craft.

{% set entries = entries.startsOn(craft.app.request.getParam('departure')|date("Y-m")) %}

That does not work!

Any ideas gratefully received!

2

You could do this:

{# Your date 2020-11 in atom format e.g 2020-11-01T00:00:00+01:00 #}
{% set departureDate = craft.app.request.getParam('departure') | date("Y-m") | atom %}

{# Your date + 1 month in atom format e.g 2020-12-01T00:00:00+01:00 #}
{% set untilDate = departureDate | date_modify('+1 month') | atom %}

{# Query between those 2 dates #}
{% set query = query.startsOn(['and','>=' ~ departureDate, '<' ~  untilDate ]) %}

Note that on the November the 25th for example, you'd only be querying 5 days of the month

1
  • Hey @oli ah ok, that's cool, that is what I did in the end, but it felt like maybe i could have been able to do it the way I hoped!! Cheers!! – John Nov 26 '20 at 15:20
1

You should be able to use the date string you get from your parametern without even editing it.

{% set entries = entries.startsOn(craft.app.request.getParam('departure')) %}

More information about querying on datefields can be found here: Date Fields | Craft CMS Documentation | 3.x

2
  • I'm specifically looking to do just the year and month, not the day as well! – John Nov 26 '20 at 12:16
  • You could try to slice the date string to only display the year and month (date|slice(0,7)). I'm not sure if this would be a valid parameter. If not, you could try to use this as parameter: ['and', '>= date|slice(0,7)-01, '<= date|slice(0,7)-31'] – Joorren Nov 26 '20 at 13:48

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.