Filter by date field

I can only find old answers that don’t seem to work… I have a structure field for a “calendar” which contains a date field. I want the calendar to only show events that are either today or in the future. My current code looks like this:

<?php $rows = $page->agenda()->toStructure();
if($rows->isNotEmpty()):?>

  <?php foreach( $rows as $row): ?> // I figure there should be some sort of filtering here?
    // Calendar items go here
  <?php endforeach ?>

<?php endif; ?>

This should help: Filtering compendium | Kirby CMS

1 Like

Thanks this definitely helped a lot! The only problem I have atm is that only future events show and not the ons of today. I’ve filtered the rows like this:

$rows = $page
->agenda()
->toStructure()
->filter(function ($child) {
    return $child
    ->datum()
    ->toDate() >= time();
  });
  

time() returns the current timestamp (that is how many seconds have passed since 1.1.1970). You are comparing the current time (eg. 3. march 2020 13:16:55) to the time of the “date” at midnight (eg. 3. march 2020 - 00:00:00). Therefore, time() will most likely always be greater than the “time of last midnight”.

I’d use this instead:

$rows = $page
  ->agenda()
  ->toStructure()
  ->filterBy('datum', 'date >=', 'today')
3 Likes

This solved the problem thank you! :slight_smile:

1 Like

Thanks you :slight_smile: