WWW FAQs: How do I place a date and/or time stamp on my page?

2005-07-15: "Time and date stamps" that change every day are obnoxious, of course, if your page is not really updated daily. Your users will figure that out, and they will tune you out.

However, if you are making daily updates, you can save yourself a bit of time and effort by automatically displaying the current date as part of the page.

There are two simple ways to do this: server side includes, and PHP. Server side includes are a little bit easier, but can only display the date and time in the time zone of the web server itself, or GMT. If neither of these is correct for your purposes, read on for our PHP solution. (Similar solutions are possible with other server-side programming technologies like ASP, Cold Fusion, and so on. These two are the most universally available.)

Date and Time Stamps With Server Side Includes

The following server side include directive will work on just about any web hosting account, with the exception of some free accounts:

<!--#echo var="DATE_LOCAL"-->

The date and time will appear correct for the time zone in which the server operates. You can also output the current Greenwich Mean Time, and set the date and/or time format you wish to use. For instance, you can display the date only (without the time) by setting the date format like this:

<!--#config timefmt="$y $m $d"-->

For more information, see the excellent SSI Developer site.

Date and Time Stamps With PHP

What if your web server is in a different time zone, and you want to display your the date and time in the time zone where you (the webmaster, not the user) are located? Unfortunately, SSI can't do it. However, PHP can. PHP, like SSI, is a way to dynamically add content to your web pages. Unlike SSI, PHP is a full-featured programming language. But taking advantage of useful snippets of PHP code doesn't require programming experience.

The following PHP code, when inserted in an HTML page that has been given a filename ending in .php (not .html), will display the current date and time in New York City:

echo date("Y-m-d h:i:sa", time());

By default, the date function of PHP also outputs the date and time for the web server's time zone. But the putenv function changes this by setting the effective time zone to the city we want. Linux users can find a list of valid time zone names in the /usr/share/zoneinfo directory (there are subdirectories for continents and so forth). You can leave out the date by skipping the Y-m-d part, and you can leave out the time by skipping the h:i:sa part. For more information about these formatting characters and what else can be done with them, see the PHP manual page for the date function.

Legal Note: yes, you may use sample HTML, Javascript, PHP and other code presented above in your own projects. You may not reproduce large portions of the text of the article without our express permission.

Got a LiveJournal account? Keep up with the latest articles in this FAQ by adding our syndicated feed to your friends list!