Skip to content

Kirby 3.9.8

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 IntlDateFormatter PHP class provided through our intl date handler is the way to go. You can change the date handler in your config.

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

If you change the date handler, you have to adjust existing date format strings in your templates. Find out more about ICU Date formats

In your template …

<?= $page->published()->toDate('EEEE, MMMM d, YYYY') ?>

Instead of passing a format string, you can also pass an IntlDateFormatter object as argument to the toDate() method:

<?=  $page->published()->toDate(new IntlDateFormatter( "de_DE", IntlDateFormatter::FULL, IntlDateFormatter::SHORT, 'Europe/Berlin')); ?>

For details, see the IntlDateFormatter PHP class