$page->modified return the created date instead of the modified date

Kirby 2.3

I have a page txt content file that is modified today 2016-05-20.

In my template I added:

echo $page->modified('Y-m-d');

It prints 2016-05-19. That’s not when it was last updated. That’s the date when it was created.

Is it a bug or should I use another function?

How did you modify the page? It works if the page is modified via the panel, not if you modify it manually.

Thanks!

I modified it, not manually but with a script that I wrote.

Do you know what the panel did, that I did not do? I mean, where is the modified date stored when saving in the panel?

I think it’s done via $page->touch(). It’s still strange, though.

1 Like

Alright.

I’ve figured out a solution that seems perfect in cases like this:

echo date('Y-m-d', filemtime( $page->textfile() ) );

Yeah, the reason is that $page->modified() gets the modification date of the folder rather than the textfile. And if you change the textfile via script or manually without touching the folder, the modification date does not get updated.

Ahh! That explains it. :slight_smile: Good info!

In my case I see no reason to edit the folders just because the text file has changed.

Solution

With the new cool Kirby features I was able to make the call even shorter in the template…

In the template:

echo $page->updated('Y-m-d');

In a plugin:

page::$methods['updated'] = function($page, $format) {
  return date( $format, filemtime( $page->textfile() ) );
};

(It seems like I can’t overwrite the original modified page method, but updated is fine for me)

Updated

I added $format to make the method work as expected.

1 Like

Yes, that’s right. If you want to overwrite existing methods, you would have to use a page model. Would probably not make sense in your case, though, if you want that method to be available everywhere.

1 Like