How to use PHP display the date a page was last modified?
Hi All,
I am trying to display the date a page was last modified by using PHP. I am using a piece of code I read about somewhere. When I put it on my site, the same code produces different results on different pages.
The code below is the PHP code that I am using.
Code:
<?php
//Display the date a page was last modified
echo "Last modified on: " . date(" m F Y ", filemtime( $PATH_TRANSLATED ));
?>
I get the result below on one page.
Quote:
Last modified on: 12 December 1969
And I get the result below on another page.
Quote:
Notice: Undefined variable: PATH_TRANSLATED in /usr/local/apache2/htdocs/demo/footer.php on line 22
Last modified on: 12 December 1969
My site is not that ancient and was not built in 1969. How can I correct that error and make it display properly please? Or can someone help me with a better code to achieve this please?
Thanks in advance.
Menre
Re: How to use PHP display the date a page was last modified?
Check on how you're getting $PATH_TRANSLATED - something is failing there. In the second case, PHP is explicitly telling you it's undefined. When it's undefined or it's empty, you get this 1969 date.
You could also use getlastmod() instead, which doesn't require passing a path (but you need it to be called on the page you want to get the mod date for - so it won't work correctly for the parent if you put it on an included file).
Re: How to use PHP display the date a page was last modified?
In my thread here I used the code SambaNeko linked to. However, I noticed that the getlastmod() function on it's own returned the wrong time.
Until I put:
PHP Code:
<?php
date_default_timezone_set('your country/city');
?>
So it would look like this:
PHP Code:
<?php
date_default_timezone_set('your country/city');
echo " " . date ("F d Y H:i:s.", getlastmod());
?>
Re: How to use PHP display the date a page was last modified?
it's not the "wrong time," it's the server's time.
Re: How to use PHP display the date a page was last modified?
Hi All,
Thanks to everyone that responded to my post. I followed your suggestions and was able to get it to work well.
I modified the code you posted to this.
Code:
<?php
//date_default_timezone_set('');
echo "Last updated " . date ("d M Y", getlastmod());
?>
Then the result was perfect as the code displayed what I wanted it to:
Quote:
Last updated 06 Dec 2009
Once again, thanks.
Menre
Re: How to use PHP display the date a page was last modified?
I think you should change your time Jone, and may be any error in date function.
Re: How to use PHP display the date a page was last modified?
I don't know why you're making useless posts in old threads.
I almost wonder if you're a bot, since you're reviving old threads with random sentences and your signature is two separate links to the same website boasting "free sms."