Filtering events with startdate and endtime

Hi, I’m struggling with the following: I have an event list (each have a start date+time and an end date+time) and I want to use de filterBy() method to only show events that:

  • Are in the future
  • Are today, have started but haven’t ended yet

I’ve tried the following without succes:

$agenda = page('agenda')
->children()
->listed()
->filterBy(function ($callback) {
    return $callback->dateStart()->toDate('ymd') >= date('ymd') && $callback->dateStart()->toDate('H:i') >= date('H:i') && $callback->dateEnd()->toDate('ymd H:i') > date('ymd H:i');
})
->sortBy('dateStart', 'asc')

I might be going about this completely wrong… I’ve tried to compare them with time() but figured that echos a unix timestamp. I read in the docs that using strtime() is deprecated so that might not be the way to go… Any help is much appreciated:)

The problem is that you are expecting all conditions to be true, but you need an conditions that checks if either in the future > date('ymd'), or date is today (ie. === date(‘ymd’) and then it should not have ended yet.

1 Like

Not sure if I understand what you mean here… :grimacing:

Can’t this be reduced to “all event’s that haven’t ended yet”?

$agenda = page('agenda')
->children()
->listed()
->filterBy('dateEnd', 'date >', 'now')
->sortBy('dateStart', 'asc')
1 Like

You’re right! Way easier like this, thank you:)

In case you also want to exclude events which have “started before today” (idk, you don’t want events that have started yesterday, even if they’re still going), you might also add that as condition:

$agenda = page('agenda')
->children()
->listed()
->filterBy('dateEnd', 'date >', 'now')
->filterBy('dateStart', 'date >=', 'today')
->sortBy('dateStart', 'asc')
1 Like