SPIP - Contrib

[ar] [en] [es] [fr] [it]



Home page > Date and Time processing

Display date and time in the visitor’s timezone

Thursday 24 March 2005, by Julien. Latest addition Thursday 25 January 2007

All the versions of this article:


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

texte - 3.3 kb

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.


Reply to this article



Follow-up of the site's activity RSS 2.0 | Site Map | Private area | Charte et vie SPIP-Contrib | SPIP | L'autre.net