Display date and time in the visitor’s timezone

If you have an international website, it can be interesting to display date and time of articles in the visitor’s timezone. This SPIP filter and JavaScript achieves this.

Howto

Download the above script, rename it as spip_localtime_en.js, upload it to the root directory of your server and write the following code in the HEAD section of your templates:

<script language="JavaScript" type="text/javascript" src="spip_localtime_en.js"></script>

Then, paste this code in yoyr mes_fonctions.php3 file:

function localtime_js($date, $format = "Y-m-d H:i:s") {
    return "<script language=\"JavaScript\" type=\"text/javascript\">s_lt(".strtotime($date).", '".str_replace('\\', '\\\\', $format)."');</script><noscript>".date($format, strtotime($date))." UTC</noscript>";
}

Eventually, place this code in your templates:

[(#DATE|localtime_js{'y-m-d H:i'})]

This date format between brackets is quite the same as function date in PHP. The supported codes are: d, F, G, H, i, j, l, m, n, s, Y and y. It also supports anti-slashes to escape characters.

Limits

Server timezone needs to be UTC (or the script needs some modifications).

If the browser doesn’t support JavaScript, date and time are displayed in UTC.

updated on 25 January 2007

Discussion

Une discussion

Comment on this article

Who are you?
  • [Log in]

To show your avatar with your message, register it first on gravatar.com (free et painless) and don’t forget to indicate your Email addresse here.

Enter your comment here

This form accepts SPIP shortcuts {{bold}} {italic} -*list [text->url] <quote> <code> and HTML code <q> <del> <ins>. To create paragraphs, just leave empty lines.

Add a document

Follow the comments: RSS 2.0 | Atom