filterBy with condition

Hi
I’m trying to filter my events based on a date field. I want to show only events in the future. With the ->filterBy('date', '>', time()) that works just great.
But my problem is that if the event has an enddate, I want to hide it after that date. But not after the startdate. And the enddate is only filled in, if the event lasts more than one day. So on regular one-day-events there is no enddate but only a startdate.
I also tried with a filter function

$page
  ->children()
  ->visible()
  ->filter(function($child) {
    if($child->dateend()->isEmpty()):
      return $child->date() > time();
    else:
      return $child->dateend() > time();
    endif;
  })
  ->sortBy('date', 'asc');

But it doesn’t seem to work. I’m sorry for my very basic PHP-skills.

This should work:

$page
  ->children()
  ->visible()
  ->filter(function($child) {
       return $child->date() > time() or strtotime($child->enddate()) > time();
  })
  ->sortBy('date', 'asc');

Thanks @texnixe for your solution. And sorry for not labeling it as solved. I haven’t had the time to do so.

But what I didn’t understand is, why do I have to strtotime() it for my $enddate, but not for my $date? They are both stored in the same format (YYYY-MM-DD).

The date field is a special field. It works just like every other field, but it has some custom code that can be used to format dates in different formats (default is the timestamp value).

You could replace the strtotime($child->enddate()) with a $child->date(null, 'enddate') to make use of it for the other field.

OK, thanks. That helps.