Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

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:

$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.



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

On 2 November 2011, Frederick Rubio (.net Development) wrote: Hyperlink chain icon

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


On 2 October 2012, Alastair wrote: Hyperlink chain icon

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.


    On 19 January 2019, Daniel Msanii wrote: Hyperlink chain icon

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


On 11 April 2013, Duane wrote: Hyperlink chain icon

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


On 29 August 2013, Deep wrote: Hyperlink chain icon

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.


On 21 November 2013, Harish wrote: Hyperlink chain icon

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?


On 29 March 2016, Bsl wrote: Hyperlink chain icon

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


On 20 August 2017, Victor wrote: Hyperlink chain icon

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


On 26 March 2018, Manjari Agarwal wrote: Hyperlink chain icon

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.


On 13 September 2020, Hajrudin wrote: Hyperlink chain icon

Thanks for the code, I just needed this!


On 19 January 2021, Yoonki Kim wrote: Hyperlink chain icon

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.


    On 29 January 2021, Adam Dimech wrote: Hyperlink chain icon

    Hello Yoonki,

    This blog post is almost a decade old. The code I use now is:

    $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.


      On 28 January 2022, Diego wrote: Hyperlink chain icon

      Hi. And… how we change de language? (days, months….) Thanks


On 29 January 2022, Diego wrote: Hyperlink chain icon

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…


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.