Skip to content

Use PHP to insert an automatic “last modified” date into web pages

G30th October 2011

C9 Comments


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:

$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 (&#116) instead. Therefore the word “the” could be included as either \t\h\e or &#116;&#104;&#101;. 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.



9 responses to “Use PHP to insert an automatic “last modified” date into web pages”

  • Written by Frederick Rubio (.net Development) on 2 November 2011:

    Hi! Your post is very informative for php developer and specially for students. thanks for sharing the information with the practical example.

  • Written by Alastair on 2 October 2012:

    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.

    • Written by Daniel Msanii on 19 January 2019:

      Thanks Alastair, you saved me on the double \\ to escape the t. Generally works great.

  • Written by Duane on 11 April 2013:

    Wow! In the end I got a web site from where I be able to truly get useful data regarding my study and knowledge.

  • Written by Deep on 29 August 2013:

    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.

  • Written by Harish on 21 November 2013:

    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?

  • Written by Bsl on 29 March 2016:

    But how do i set the Time Zone to match my country here ??

  • Written by Victor on 20 August 2017:

    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));

  • Written by Manjari Agarwal on 26 March 2018:

    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.

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.