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') ?>

The date field

If you use the field name date for your dates, you can simplify this step:

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

As an argument you can pass any valid php date format string. If you don't pass any string the function will return a unix timestamp, which you can use to make some more fancy php date calculation stuff.

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 are working with 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') ?>