Filtering events by date

I’m trying to create an overview page with the children being events. There can be multiple events on one day and I want to create a heading containing the date and a filter of the events on that day if the events exist. Example:

Thursday 18th of August

  • Event 1
  • Event 2

Sunday 21st of August

  • Event 1

I can’t figure out how to make this happen though… I know how to filter if the event date is in the future:

<?php 
$events = $page
->children()
->listed()
->filterBy('datum', 'date >=', 'today'); if ($events->count() > 0): ?>
  <ul>
    <?php foreach ($events as $event): ?>
    <li class="event">
      <a href="<?= $event->url() ?>">
        <?= $event->title() ?>
      </a>
    </li>
    <?php endforeach ?>
  </ul>
<?php endif ?>

Can anyone point me in the right direction? Cheers!

You would want to group them instead of filter, check out this recipe: Grouping collections | Kirby CMS

1 Like

Wow Kirby keeps on amazing me!

The output for the date field is now something like: 2022-08-18. Is it also possible to make it something like: Thursday 18 August?

I’ve seen this article but it doesn’t seem to help me…

<?php $days = page('programma')->children()->listed()->filterBy('datum', 'date >=', 'today')->groupBy('datum'); ?>

<?php
foreach($days as $day => $itemsPerDay): ?>
    <h2><?= $day ?></h2>
    <ul>
      <?php
      foreach($itemsPerDay as $item) : ?>
      <li><?= $item->title() ?></li>
      <?php endforeach; ?>
    </ul>
<?php endforeach ?>

You can use the toDate() method for that: $field->toDate() | Kirby CMS

Yes but where should the formatting of the date take place since I can’t do ->toDate() on a string… (sorry quite new to all this so I really appreciate the help!)

Maybe you could first convert the string into a timestamp with PHP: strtotime - Manual and then format that with PHP: date - Manual

Thanks for your quick response! To be honest I have no clue how to do so…

Do you know if there is a way to format the date correctly before grouping it?

Isn’t that the solution: Grouping collections | Kirby CMS

Go to the Complex grouping part and replace the'Y' in the toDate() with the formatting you want.

1 Like

Perfect that did the trick! The only problem I’m left with now is the language of formatting… I’ve added this to my config.php:

return [
    'date.handler' => 'strftime',
    'locale' => 'nl_NL.UTF-8',
];

and the code I am using for formatting is

datum()->toDate('%A %-d %B %Y');

which outputs Thursday 18 August 2022 instead of Donderdag 18 augustus 2022

Make sure the locale as given is installed on your machine, in Terminal locale -a

It might make more sense to use the intl driver, as strftime is deprecated.

It is… But is not doing anything. Also went to everything from this thread but having the same problem

Are you in a multilanguage installation or single language. And your locale on your machine is exactly like given in your config?

Switching to intl solved my issue:)