Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Displaying local time without multiple caches

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
Tuxie
Smarty n00b


Joined: 14 Sep 2004
Posts: 4

PostPosted: Wed Sep 15, 2004 6:15 am    Post subject: Displaying local time without multiple caches Reply with quote

I just wanted to share a little trick I have for my site. It let me have a single cached page with all times/dates shown in the user's local time, the timezone that is configured by her operating system.

This is the first thing I've written in JavaScript ever so this could probably be made both cleaner and more efficient by someone who know JavaScript better. Smile

The file "localtime.js" is included in my template's header using:
<script type="text/javascript" src="js/localtime.js"></script>

Then a dynamically generated javascript file is included with:
<script type="text/javascript" src="config.js.php"></script>

config.js.php outputs variables like:
_timeformat = 'Y-M-D H:m';
or
_timeformat = 'M/D/y h:ma';

In my database I store ALL timestamps as GMT/UTC UNIX timestamps/epoch integers.

localtime.js:
Code:

/* localtime.js by Per Wigren <tuxie(a)dekadance(.)se> */

function zeropad(number)
{
        if( number < 10 ) return ("0"+number);
        else return number;
}
function ampm(hour)
{
        if( hour < 12 ) return "am";
        else return "pm";
}
function hour12(hour)
{
        if( hour > 12 ) return hour - 12;
        else return hour;
}

function localtime(epoch)
{
        d = new Date(0);
        d = new Date( d.setUTCMilliseconds(epoch*1000) );
        str = _timeformat.replace('Y', d.getFullYear());
        str = str.replace('y', d.getYear());
        str = str.replace('M', zeropad(d.getMonth()+1));
        str = str.replace('D', zeropad(d.getDate()));
        str = str.replace('H', zeropad(d.getHours()));
        str = str.replace('h', hour12(d.getHours()));
        str = str.replace('m', zeropad(d.getMinutes()));
        str = str.replace('s', zeropad(d.getSeconds()));
        str = str.replace('a', ampm(d.getHours()));
        document.write(str);
}


Then I wrote a Smarty modifier plugin that looks like this:

Code:

function smarty_modifier_localtime($timestamp)
{
        return '<script type="text/javascript">localtime('. $timestamp .');</script>';
}


Now, every time I use {$timestamp|localtime} in a template Smarty will replace it with:
<script type="text/javascript">localtime(123456789);</script>

I use this technique for more things that frequently change or are user-specific. My dynamically created config.js.php outputs variables and my generated html write them like <script type="text/javascript">document.write(_myVariable);</script> instead of hardcoding them.

I hope someone find this useful. Wink
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Wed Sep 15, 2004 6:22 am    Post subject: Reply with quote

Why dont using the date_format with an config var?

You can let the user change between the files which will be used for formating the date.

Remeber:
Javascript can be disabeld on browserside an so your page wont looks good.
Back to top
View user's profile Send private message
Tuxie
Smarty n00b


Joined: 14 Sep 2004
Posts: 4

PostPosted: Wed Sep 15, 2004 8:55 am    Post subject: Reply with quote

My site will require JavaScript to work correctly anyway. If JavaScript independance is required the approach won't work. The site will still work, but timestamps and live statistics will be shown as "####" or something instead, adding <noscript>####</noscript> to the Smarty modifier.

This method let me have ONE cached version of the pages instead of 20 versions per page, which means 1/20 as many page-generations which means that a LOT less server-power is required to run the site. And the site will still be very user-configurable. I think it's worth it.
Back to top
View user's profile Send private message
sage
Smarty Rookie


Joined: 09 May 2004
Posts: 20
Location: London, UK

PostPosted: Wed Sep 15, 2004 11:14 am    Post subject: Reply with quote

Quote:
My site will require JavaScript to work correctly anyway.
Shocked


If you want to change the date using the client side browser (which is a good method), instead of using a direct document.write method, use default text such as the servers local time, which you change.

For example, for the date, you could place in your html somthing like:

Code:

<span id="usr_time">Server Local Time {$smarty.now|date_format:"%H:%M:%S"}</span>


Then with the js, you can do somthing like (maybe a bit more neatly though Wink )

Code:

window.onload{
    var date_text = document.getElementById('usr_time').firstChild;
    date_text.nodeValue = localtime();
}



Where localtime() is a js function to return the local time in a string.


This way even if the user has js disabled you have some text which makes sense, but with js, adds to the usability.

(you could use the <noscript> tag which as you said will work, but I personally think its a nasty method, and also document.write will not work if you decide to make your pages XHTML)
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP