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

// +----------------------------------------------------------------------+
// | SPIP.DateLocales.JS v.1.0.0-English                                  |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Adeixis.Communication                             |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or        |
// | modify it under the terms of the GNU General Public License          |
// | as published by the Free Software Foundation; either version 2       |
// | of the License, or (at your option) any later version.               |
// |                                                                      |
// | This program is distributed in the hope that it will be useful,      |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
// | GNU General Public License for more details.                         |
// |                                                                      |
// | To read the license, please visit:                                   |
// |       http://www.gnu.org/copyleft/gpl.html                           |
// +----------------------------------------------------------------------+
// | Author: Julien Tessier <julien@adeixis.com>                          |
// +----------------------------------------------------------------------+

function s_lt(timestamp, format) {
	if (format == undefined)
		format = "Y-m-d H:i:s"
	date_object = new Date();
	date_object.setTime(timestamp*1000);
	document.write(spipLocalTime_formatDate(date_object, format));
}

function spipLocalTime_addZero(number){ 
    return ((number < 10) ? "0" : "") + number 
} 
        
function spipLocalTime_formatDate(date_object, format){ 
    var daysTextual       = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Sunday"); 
    var monthsTextual     = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
	
	var replacements      = new Array();
	replacements['d']     = spipLocalTime_addZero(date_object.getDate());
	replacements['F']     = monthsTextual[date_object.getMonth()];
	replacements['G']     = date_object.getHours();
	replacements['H']     = spipLocalTime_addZero(date_object.getHours());
	replacements['i']     = spipLocalTime_addZero(date_object.getMinutes());
	replacements['j']     = date_object.getDate();
	replacements['l']     = daysTextual[date_object.getDay()];
	replacements['m']     = spipLocalTime_addZero(date_object.getMonth()+1);
	replacements['n']     = date_object.getMonth()+1;
	replacements['s']     = spipLocalTime_addZero(date_object.getSeconds());
	replacements['Y']     = date_object.getFullYear();
	replacements['y']     = spipLocalTime_addZero(date_object.getFullYear().toString().substring(3,4));
	
	var parsed_string = '';
	for (i=0; i

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.

Discussion

One discussion

Add a comment

Avant de faire part d’un problème sur un plugin X, merci de lire ce qui suit :

  • Désactiver tous les plugins que vous ne voulez pas tester afin de vous assurer que le bug vient bien du plugin X. Cela vous évitera d’écrire sur le forum d’une contribution qui n’est finalement pas en cause.
  • Cherchez et notez les numéros de version de tout ce qui est en place au moment du test :
    • version de SPIP, en bas de la partie privée
    • version du plugin testé et des éventuels plugins nécessités
    • version de PHP (exec=info en partie privée)
    • version de MySQL / SQLite / PostgreSQL
  • Si votre problème concerne la partie publique de votre site, donnez une URL où le bug est visible, pour que les gens puissent voir par eux-mêmes.
  • En cas de page blanche, merci d’activer l’affichage des erreurs, et d’indiquer ensuite l’erreur qui apparait.

Merci d’avance pour les personnes qui vous aideront !

Par ailleurs, n’oubliez pas que les contributeurs et contributrices ont une vie en dehors de SPIP.

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