0

Pulling hair out here so looking for divine inspiration.

Trying to list a set of events/entries grouped by day. This is fine when all the dates are organised by an event date but when we introduce a date span (event start date and event end date) am struggling to work out how to get days that are not specifically occurring on a set day to appear, eg if starts Tuesday and ends Thursday to show on Wednesday.

I can get this working with an arbitrary range, eg to show all the events across a certain month but this gets considerably more complicated introducing pagination when no date range is specified. Ideally need to paginate by dates but pagination requires the ElementCriteriaModel.

Any thoughts on how might go about this as banging head against a brick wall …

3

It's difficult to give specific code help without seeing some input/output examples, but here's a go-by:

You can say that an event occurs during a date range if:

  • The event begins before the date range ends
  • and, the event ends after the date range begins

So, the pseudocode looks something like:

  • Prepare a DateTime representation of the range rangeStartTime (e.g. today at 00:00)
  • Prepare a DateTime representation of the range rangeEndTime (e.g. tomorrow at 00:00)
  • For each event, check if the criteria are satisfied: eventStartTime < dayEndTime and eventEndTime > dayStartTime
  • If the criteria is satisfied, include the event.

So, let's say each of my Event entries has eventStartDate and eventEndDate fields, and I want to list out all the events that occur today (i.e. including, for example, events that started yesterday and end tomorrow).

I could do something like...

{% set dateRangeStart = today|date('Y-m-d') %}

{% set dateRangeEnd = tomorrow|date('Y-m-d') %}

{% set eventsInRange = craft.entries
    .section('events')
    .eventStartDate('<'~dateRangeEnd)
    .eventEndDate('>'~dateRangeStart)
%}

Now eventsInRange is an ElementCriteriaModel that finds all Event entries in our date range. And since it's an ElementCriteriaModel, you can plug it into the {% paginate %} tag to get paginated results according to whatever other parameters you desire.

0

Thanks @Michael - the difficulty is always going to be introducing some kind of pagination across dates rather than entries. Ended up hacking something together that imitates pagination. Happy to hear any suggestions on improvement, especially in terms of performance/efficiency:

https://gist.github.com/cole007/8fbe1247791985aea3e9

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.