Kirby gets the date right, but not the time

Hi there,

I’m new around here and in many ways still inexperienced, so please bear with me.

I’m trying to set up a blog with the latest version of Kirby. The blog requires a proper way to show the date and time the entry was posted. I haven’t touched the respective Kirby fields.

The theme always gets the date right, but not the time, which remains at midnight. Within the panel, the correct time is given (it’s a fairly odd one, not midnight).

I have a RSS feed as well, which also sticks to midnight, so I don’t think the theme has to do with the issue. Or does it?

Does anyone have a clue where to look at?

Many thanks in advance for your help!

Which theme are you using?

I forgot the name. It’s a theme that worked with Kirby v1 only. I customized it up to the point that almost everything was rewritten.

This is how the (allegedly) important part of the theme is written:

<?php $articles = $page->children()->visible()->flip()->paginate(5) ?>
<?php foreach($articles as $article): ?>
    <div class="post">
        <div class="post-meta"><?=$article->date('d.m.Y, G:i')?></div>
        <h3 class="post-title"><a href="<?=$article->url()?>"><?=$article->title()?></a></h3>
        <p><?php echo excerpt($article->text(), 350) ?></p>
    </div>
<?php endforeach ?>

How do you enter the date and time in the text file? Is that via a date field, or date and time fields?

Ah, good question! As far as time is concerned, I have added a blueprint, which results in separate fields on the panel for the date and the time. Now, as soon I write a blog entry, the text file includes this:

Date: 2015-06-15

----

Time: 18:54

The blog itself would show everything as expected if the time was added to the “Date:” section:

Date: 2015-06-15 18:54

How do I get the blueprint to create the blog entry correctly?

You need those two separate fields, that is correct. But in your template, you only call the date, which does not contain the time or is rather set to midnight. To show the date, simply echo the time field:

<?php echo $article->time() ?>

Amazing, it’s as easy as this, thank you! Really appreciate your quick and competent help, guys!

The only thing left to correct would be the feed, which is generated by Bastian’s plugin. Where am I to edit either the feed.php and/or the template.php?

You need to change lines 8 and 20 in the template, instead of

$item->date('r', $datefield);

echo the separate elements separately: http://php.net/manual/de/function.date.php

Not sure whether I really got this, as PHP is not my strong suit, but here goes nothing:

Line 8:

<lastBuildDate><?php echo date('r', $modified) ?><?php echo time('r', $modified) ?></lastBuildDate>

Line 20:

<pubDate><?php echo $datefield == 'modified' ? $item->modified('r') : $item->date('r', $datefield) ?><?php echo $timefield == 'modified' ? $item->modified('r') : $item->time('r', $timefield) ?></pubDate>

If I apply the changes, the date itself is turned back by nine days, while the time itself is a few hours off, indicating an incorrect time zone.

I finally came up with the solution, thanks to being sleep-deprived, which provided a much-needed sense of clarity. Aww, you know that!

In the feed.php, I have defined a new default:

'timefield'   => 'time',

This ensures that a separate field for the time within a blog post can be processed properly.

Now, looking at the template.php, line 8 does not need to be rewritten. It apparently generates the information when the feed itself was refreshed.

Line 20, however, is key. It fetches the information from the date field of the blog post and displays it in accordance to RFC 2822 (example: Thu, 21 Dec 2000 16:01:07 +0200). All I had to do is rebuild this structure piece by piece. This allows me to jump to another echo (if that makes sense), from date to time:

<pubDate><?php echo $datefield == 'modified' ? $item->modified('D, d M Y') : $item->date('D, d M Y', $datefield) ?> <?php echo $timefield == 'modified' ? $item->modified('H:i:s O') : $item->time('H:i:s O', $timefield) ?></pubDate>

This, in fact, works wonderfully. Wow.

1 Like

:thumbsup: That’s exactly what I meant. But I had another idea, just haven’t had the time to test this, i.e. using a page model to combine both date and time fields into a single field value and then go from there.