“Last Modified” text

posted in: Uncategorized | 0

Often on pages, it is useful to include the last date the document was updated. Often, people just manually change it every time a change is made. But why? Shouldn’t this be automatic?

PHP

For the last modified date in PHP, something like this should suffice…

echo "This page was last modified: " . date ("F d Y", filemtime(__FILE__));

C#

You’d wanna take this date and add it to a label or something, but you’d probably use something like that…

HttpWebRequest req =(HttpWebRequest)WebRequest.Create(Path.GetFileName(request.PhysicalPath));
HttpWebResponse res =(HttpWebResponse) req.GetResponse();
DateTime  abcd = res.LastModified;

Java

Again, do what you wish with what this returns.

String lastModified = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").parse(request.getHeader("last-modified")).toString();

NOTE: For this to work in Java, your server needs to be configured to send along the last-modified header. Many servers DO NOT by default.

JavaScript

So, as a fallback, or if you don’t want to use server side, or for whatever reason, you can also use javascript to get the last modified date of the current file.

var lastModified = document.lastModified;

Conclusion

Depending on your needs, you may use just the javascript version, one of the server side versions, or a mixture of a server side with a javascript fallback.

My name is Andrew McGivery. I currently work full time as an application developer at Manulife Financial in Canada. My current passion is building and leading highly engaged teams where employee happiness, learning, and growth is a priority.

Leave a Reply