Skip to content

Kirby 3.5.7.1

Working with dates

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.

We suggest that you stick to the format YYYY-MM-DD HH:MM:SS (i.e. 2018-11-21 11:22:33). If you want to use another date format in your text files, read more about parseable date formats in PHP.

Article example

<article>

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

  <time datetime="<?= $page->published()->toDate('c') ?>" pubdate="pubdate">
    <?= $page->published()->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'
];

If you change the date handler you have to adjust all the date format strings in your templates. Find out more about the strftime date formatting syntax

In your template …

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