Skip to content

Working with dates

Use dates in your content files and output different date formats.

Any field in Kirby can be converted to a date if the content is parseable by PHP.

Title: Super Nice Content
----
Published: 2018-11-21
----

In your template …

<?= $page->published()->toDate('d.m.Y') ?>

As an argument you can pass any valid PHP date format string. If you don't pass an argument, the function will return a Unix timestamp, which you can use for more fancy PHP date calculation.

Article example

<article>

  <h1><?= $article->title() ?></h1>
  <?= $article->text()->kirbytext() ?>

  <time datetime="<?= $page->date()->toDate('c') ?>" pubdate="pubdate">
    <?= $page->date()->toDate('d.m.Y H:i') ?>
  </time>

</article>

Custom date handler

PHP's date function is great, but if you work on multi-language sites and you need to translate dates, the strftime function is actually more powerful. You can change the date handler in your config.

/site/config/config.php
return [
    'date.handler' => 'strftime'
];

In your template …

<?= $page->date()->toDate('%d.%m.%Y') ?>