Formatting dates from a structure field

Hey there!

I’m working with a structure field that gets translated into a table. It works fine, except I am somehow unable to change the format of the date (e.g. to: DD.MM.YYYY).

<?php foreach ($page->visits()->yaml() as $visit): ?>
  <tr>
    <td><?= $visit['name'] ?></td>
    <td><?= $visit['date'] ?></td>
    <td><?= $visit['time'] ?></td>
    <td><?= $visit['keyword'] ?></td>
  </tr>
<?php endforeach ?>

Can someone give me a hint on how to do this? Thank you!

Best,
Leon

Use toStructure() instead of yaml() then you can use standard field methods and arrow syntax:

<?php foreach ($page->visits()->toStructure() as $visit): ?>
  <tr>
    <td><?= $visit->name() ?></td>
    <td><?= $visit->date()->toDate('Y-m-d') ?></td>
    <td><?= $visit->time() ?></td>
    <td><?= $visit->keyword() ?></td>
  </tr>
<?php endforeach ?>

What Kirby version? The code above applies to Kirby 3.

Thank you for your quick reply! When I replace yaml() with toStructure(), I get an “unexpected error” message. I am working with Kirby 2.5.12.

Ah, ok, then toDate() won’t work.

<?php foreach ($page->visits()->toStructure() as $visit): ?>
  <tr>
    <td><?= $visit->name() ?></td>
    <td><?= $visit->date('Y-m-d') ?></td>
    <td><?= $visit->time() ?></td>
    <td><?= $visit->keyword() ?></td>
  </tr>
<?php endforeach ?>

We have question categories that apply to each major Kirby version, makes it easier to give the right answers.

That worked! Thank you so much for your help. I will pay attention to the question categories next time.

1 Like