Get last modified date ona published object in PHP

Author Message

Alex Jones

Wednesday 19 November 2003 10:28:50 am

I need to find out when a content object was last modified with a PHP script (kernel/content view.php to be specific). I have tried a couple of methods but have not been able to generate the proper date. I have a feelign any "solution" that I come up with will be ugly and probably much more complicated than the right way.

I am trying to set the Last-Modified header for each document to the date and time that the content object was modified.

I would truly appreciate any help!

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Hans Melis

Wednesday 19 November 2003 10:49:52 am

Hi Alex,

To get the modified timestamp, just do the following in PHP:

$object->attribute( 'modified' );

This will return a unix timestamp. To format it, just use PHP's date function (and see PHP's manual for all formatting options, it's quite similar to the 'date' command in unix).

--
Hans

Hans
http://blog.hansmelis.be

Hans Melis

Wednesday 19 November 2003 10:58:58 am

In fact, you can copy the header being sent in index.php. That would format the timestamp as a GMT date/time.

header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $object->attribute('modified') ) . 'GMT' );

This will send a Last-Modified header in the format: Wed, 18 Nov 2003 18:54:19 GMT (but then with your timestamp converted to the GMT timezone).

--
Hans

Hans
http://blog.hansmelis.be

Alex Jones

Wednesday 19 November 2003 11:49:10 am

Hans, I really appreciate your help. But this doesn't seem to work. When I insert the code you furnished, eZ publish generates an error and cannot finish processing. I tried echoing the statement, but what resulted was:

'Last-Modified: ' . gmdate(modifed)

Instead of
'Last-Modified: ' . Monday, November 10, 2003 4:04:24 PM

It doesn't seem to grab the date to process... Any thoughts?

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Hans Melis

Wednesday 19 November 2003 1:33:25 pm

That's weird. I tested the code on my local install and it works fine.

Of course, $object means the name of the variable that holds your content object.

Just did another test (just printing it in kernel/content/view.php) with the code:
gmdate( 'D, d M Y H:i:s', $object->attribute('modified') )

The result of that code (on my content object) is:
Mon, 29 Sep 2003 18:43:36

EDIT: I just noticed there's a typo in my post above. It said 'modifed' instead of 'modified'. I should learn to always use copy/paste instead of typing it again ;) Sorry for that.

--
Hans

Hans
http://blog.hansmelis.be

Alex Jones

Wednesday 19 November 2003 2:09:38 pm

That did it! I understand the mistake; I've made many like that myself. In fact, I should have caught the typo as well considering how much time I spent studying that one line.

Thanks for your help!

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.