I don’t know how to create html from dates


I am building a website for an art space and they want an exhibition overview sorted by date. Each year has to be in a single row (<div>). At the beginning of each row, should be the year. I made a sketch to visualize what I want to archive. I thought about two ways to get that:

First: I write the year in the code and after that Kirby puts out the subages. The problem is, that in the future I have to change to code manually at the beginning of every year.

Second: Kirby reads the data from the date field and creates a row for every year. So when the author writes 2021 Kirby automatically builds a new <div> container for all the subpages dated with 2021.

I have skills in HTML and CSS but literally zero in PHP. I don’t know the syntax or how to read it. Mostly I learned from the YouTube tutorials by Bastian Allgeier. So I asked my Dad how he would solve this problem and he wrote the following code. Is this correct, or does Kirby have other (easier) ways to get what I want?

$alleAusstellungen = $page->children()->sortBy("date","desc"); //
2020, 2019, 2019, 2018
    foreach ($alleAusstellungen as $meineAusstellung ) {
        $myDate = $meineAusstellung->date(); // liefert das Datum aus  dem Metafile 2020-03-18 12:00:00
        $myYear = date("Y", $myDate );

        if($myYear != $oldYear ) {
            if( $oldYear != "" ) {
                // dann ist es die 2., 3..n.  Zeile
                echo "</div>";
            $oldYear = $myYear;
            // neue Zeile beginnen
            echo "<div>";
            $meineAusstellung->image(); // 1. Bild einer Zeile
            // ein weiteres Bild hinzufügen
            $meineAusstellung->image(); // n. Bild einer Zeile

Kirby has the group method to make this quite straightforward:

// group exhibitions by year
$exhibitionGroups = $page->children()->sortBy("date","desc")->group(function($p) {
  return $p->date()->toDate('Y');
// loop through groups
foreach($exhibitionGroups as $year => $itemsPerYear): ?>
    <h2><?= $year ?></h2>
       <!-- loop through items of each year group -->
      <?php foreach($itemsPerYear as $item) : ?>
      <li><?= $item->title() ?></li>
      <?php endforeach; ?>
<?php endforeach ?>

Change HTML as needed.

Don’t ever echo HTML tags in PHP.

This worked perfectly! Thank you!

Is there an easy way to make a 19 out auf 2019? And 20 out of 2020 and so on…?

Sure, you can change the date format from Y to y (see https://www.php.net/manual/de/function.date.php)

$exhibitionGroups = $page->children()->sortBy("date","desc")->group(function($p) {
  return $p->date()->toDate('y');
1 Like