Have you ever had a situation with a PHP site, where you are including document files to display on the site and need to display the recent revision date for the included file?  If you’ve ever quickly tried to accomplish this you may have discovered it’s not immediately obvious how to achieve it.  Here’s the answer.

I have worked with online genealogy since the early days of the internet, and it’s not uncommon for some documents to require periodic updating.  If you care about your site visitors (and who doesn’t?) you’ll want to provide a date for when that information was last revised which is a huge help to those doing research.  Way back when all things were HTML (with the exception of PERL of course) this was done on a by-hand per-file basis, manually editing that revision date each time you made a change.  And of course forgetting to change that date at times.

There was something called SSI (Server Side Includes) that could be used for things like this, but because of the inherent potential security issues with SSI not all server administrators wanted to enable that.  Today with modern web programming it’s no longer required and SSI is rarely used.

You may be thinking at this point “why bother including text files with PHP when everything could easily be in a database?”.  And that’s a valid point.  For one thing, if you have these documents on your computer and are maintaining them in that manner (as genealogists do), why add the extra trouble of updating a database each time you make a change when you can simply upload the document?  No to mention you may be able to argue including a file is slightly quicker than a database call and the extra overhead that places on a server.  For me it’s easy, allows good page load speeds, saves time, and compliments my finely tuned laziness.

A more relevant example most web site developers can relate to is in the case of a web site privacy policy which we all need to be concerned with these days.  They need updating from time to time and you should post that date clearly.  This is another good example of when NOT to put something in a database.  Privacy policies are typically very wordy requiring a lot of space, never need to be indexed or searched against similar bits of data (you only need one or maybe two policies right?), are not usually updated too often, and would place an unnecessary burden on your database with absolutely no benefit.  A simple HTML formatting does the trick.

So you’ve got your privacy policy and you want PHP to display the revision date, which in this case we’ll be using the server modified file date.  For that we will call the PHP filemtime function and place it within your privacy policy file at the location you want that date displayed.  The catch lies in determining the path and file name to provide to this function.  Unless you’ve managed to memorize the entirety of details within the PHP language, you probably need to do a quick search for a functionality such as this.  You may first think to turn to the PHP $_SERVER variables and maybe try using $_SERVER[‘PHP_SELF’].  This however is NOT going to do what you are after because that variable will point to the current file that is running the script and not your included file.  In fact none of the $_SERVER variables are really going to help you with this.

What we need are the PHP Magic constants and in particular the __FILE__ constant which returns “The full path and filename of the file with symlinks resolved.”.  While looking into this I ran across this excellent post on Stack Overflow that clearly explains the various options we have for discovering a path to a file.  Here we see __FILE__ will provide the correct information we need to send to filemtime function and that looks like this

filemtime(__FILE__)

We still need to format that date so it makes sense, and with that we can place the following code in our privacy policy file at the place we want that modified date to appear.

date ( "M d, Y h:i a", filemtime(__FILE__) )

Done!  If all you have is a single file to post a revision date for you can stop here.

But what if you have multiple files, and you want one function you can copy and paste across all the various files you want a revision date on?  Continue reading.

That is what I had.  Numerous files needing a revision date, and I also wanted the same CSS formatting across the site as well.  To make life a lot easier I quickly developed the following function and CSS to allow me to just copy and paste one small bit of code in all files needing a revision date.  This way should the formatting need to change, a quick change to the CSS takes care of every instance across the entire site.  No need to edit each page.

The function, which includes all formatting:

// File Updated Stamp
function UpdateStamp($file) {
	echo '<p class="datestamp">Updated ' . 
	date ( "M d, Y h:i a", filemtime($file) ) . 
	'</p>';
}

The CSS:

.datestamp {
	font-family: Arial;
	font-size: 0.75rem;
}

Lastly, the code in the file(s) needing a revision date:

<?php UpdateStamp(__FILE__); ?>

You’ll have a uniform revision date in every file you need it in, that can be reformatted easily across all files with a slight change in either the CSS or the function.

Follow comments on this post with this RSS 2.0 feed.
Leave a comment, or trackback from your own site.

You must be logged in to post a comment.

. . :   design & hosting by creed3.com   : . .