Displaying the Correct Time for Your Time Zone

Let’s say that you want to display the time on your HTML page.  You could do it like this in test.html:

<html>
<body>
<!--#config timefmt="%I:%M %p %Z" -->
<p><!--#echo var="DATE_LOCAL"--></p>
</body>
</html>

That works fine as long as your ISP is in the same time zone as you are.  But what if it isn’t?  You could try changing the time zone in .htaccess like so:

SetEnv TZ America/Los_Angeles

Unfortunately, this does not work with SSI directives, at least not on my host.  The above test.html file will display 08:02 PM CDT because my host is in Texas.  The above SetEnv does, however, work with PHP.  Fortunately, if you’re set on using HTML files, there’s a way to use SSI to call a PHP file.  The new test.html looks like this:

<html>
<body>
<p><!--#include file="display-local-time.php" --></p>
</body>
</html>

display-local-time.php looks like this:

<?php
echo date("g:i A T e");
?>

Now, test.html will display 6:02 PM PDT America/Los_Angeles.

References:
http://support.hostgator.com/articles/hosting-guide/hardware-software/can-i-change-the-server-clock-or-time-zone
You can find a list of supported time zones to set in your .htaccess file here: http://www.php.net/manual/en/timezones.php
The parameters for the PHP date function are listed on the reference page here: http://us.php.net/manual/en/function.date.php

This entry was posted in Apache, HTML, PHP and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.