Use PHP to insert an automatic “last modified” date into web pages
Use PHP to generate a “Last modified” date into your static web pages that automatically updates when the page does, so you don’t need to worry about it.
When one is presenting factual information within a website, the date that the information was published is of critical importance so that a user can determine the relevance or accuracy of that information. Data or information that was accurate in 2004 may not be so today. Therefore, a “last modified” date is important for information-based websites.
Whilst a web designer can add a “last modified” date manually to a static PHP page, it seems more logical to automate the process so that the date and time are automatically updated when the file is.
The problem with many PHP scripts that utilise the date()
function in PHP to echo the “last modified” date is that one is required to insert the file name manually. This is not efficient nor practical in most situations as the code has to be changed for every page.
Instead, the following code can be used to automatically publish a “last modified” date within any web page:
<?php
$file = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $file);
$pfile = $break[count($break) - 1];
//echo $pfile;
echo "This page was last modified on " .date("d F Y \at g:ia",filemtime($pfile));
?>
The output of this code would be in the format “This page was last modified on 30 October 2011 at 2:23pm“.
The first part of this code determines the file name of the page in which it is included, so that the code snippet can be included in any page on your web server.
The format of the date()
output can be easily modified by altering the PHP date string parameters. You will notice that “at” has been uniquely formatted. Both a and t are date string parameters and need to be escaped with a backslash so that they display as “at” and not two more variables.
For some reason, \t does not display as the letter “t” within date()
on my web server (which uses PHP 5.2.17 ) so I have included it as an HTML entity (t) instead. Therefore the word “the” could be included as either \t\h\e or the. The former is obviously the preferred solution. You need only use a backslash to escape characters that are date string parameters. So for instance, you would not need to escape the letter “b”.
To make this task really easy, I suggest that a PHP file be made with the “date modified” code and stored centrally as “date-modified.php” on your server. The file could then be served as a PHP include within individual pages:
Page last modified: <?PHP include("/path/to/date-modified.php");?>
The final aspect to consider is that this code will display when the page was modified, not necessarily the information contained within. So for example, your correction of a small typographical mistake would cause the date to be revised. Therefore, you need to consider whether this meets your requirements or not.
Comments
14 responses to “Use PHP to insert an automatic “last modified” date into web pages”
Hi! Your post is very informative for php developer and specially for students. thanks for sharing the information with the practical example.
Hi Adam.
While inside inverted commas(“), your \t is being interpreted as a tab-space (think \n for newline and \s for space). If you use \\t to escape the slash, it will display as “t” or you can use date(‘\t’) with commas(‘).
Alastair from Sydney.
Thanks Alastair, you saved me on the double \\ to escape the t. Generally works great.
Wow! In the end I got a web site from where I be able to truly get useful data regarding my study and knowledge.
Hi Adam,
I was searching for a way to display last modified date on each of the page and I reached here. Code is working perfectly!!
This is a simple stuff, but its quite dynamic and easy to integrate the way you have demonstrated.
Keep up the good work.
Thanks
very good code.. working perfectly.. How to do tis on a simple htmal page? is it possible without saving the html page with .php extension?
But how do i set the Time Zone to match my country here ??
Excellent, thank you!
As to that \at – you guys better remove it at all. Like this:
echo “Last modified on ” .date(“M d Y g:ia”,filemtime($pfile));
very good code.
Thank you so much.
As i use this code in my website and it works properly, but if I add some content from the database and then the page will no show last updated of that day.
I mean it only works for page changes not for content changes through database. Any idea how to implement both.
Thanks for the code, I just needed this!
Bye!
Thanks for sharing your knowledge.
I put your code into a function (changing echo to return) and it look working great.
But I tried change formats etc. a few times (after first deploy) and found the time and date stayed same.
I don’t understand why.
Please share your wisdom.
Thanks again.
Hello Yoonki,
This blog post is almost a decade old. The code I use now is:
<?php
date_default_timezone_set('Australia/Melbourne');
$file = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $file);
$pfile = $break[count($break) - 1];
echo "" .date("d F Y \at g:ia",filemtime($pfile));
?>
Change your time-zone as appropriate.
It’s difficult to say why the time doesn’t update. Is there server caching? Are you sure that you really updated the file that you’re looking-at? I overhauled this website in December 2020 and used the code above and it all works nicely.
Hi. And… how we change de language? (days, months….) Thanks
I made a simple question and didn’t approve my message? ok… I’ll search the information on another page to change de months language…