Display Server Time

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm

Display Server Time

Post by Nullig »

Here's a little code snippet I use to show the current server time in the footer section of my site. It has a continuous update feature, using javascript.

Copy this code to a file, name it "servertime.php" and upload to your server:

Code: Select all

<?php
?>
<__script__ type="text/javascript">
<!--
function clock() {
    document.write('<span id="clock"></span>');
    // here's the PHP code that spawn out the server local time
    <?php list($sy,$sm,$sd,$sa,$sh,$si,$ss) = explode(",", date("Y,n,j,a,g,i,s")); ?>
    // now we represent the server time as javascript date
    var server = new Date(<?=$sy?>, <?=$sm?>, <?=$sd?>, <?=$sh?>, <?=intval($si)?>, <?=intval($ss)?>).getTime();
    // calculate the client time
    var client = new Date().getTime();
    // run our ticker
    tick(client-server);
}
function tick(diff) {
    var d = new Date(new Date().getTime()-diff);
    var i = d.getMinutes(); if(i < 10) i='0'+i;
    var s = d.getSeconds(); if(s < 10) s='0'+s;
    var clock = document.getElementById("clock");
    if(clock) {
        clock.innerHTML='Current Server Time: '+
            d.getHours()+':'+i+':'+s+' <?=$sa?>';
        setTimeout('tick(' + diff + ');', 1000);
    }
}
//-->
</__script>
Note: You can change the message text 'Current Server Time: ' to whatever you like.

Within the HEAD section of your template add:

Code: Select all

{php} require_once('servertime.php'); {/php}
Note: I uploaded the servertime.php file to the root directory. If you put it anywhere else, be sure to add the proper path in the above code.


Place this code where you want the clock to show - I put mine in the footer:

Code: Select all

{literal}<__script__ type="text/javascript">clock();</__script>{/literal}
Nullig
cyberman

Re: Display Server Time

Post by cyberman »

Thanx for that nice gimmick :)
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm

Re: Display Server Time

Post by Nullig »

And if your local time is different to your server's time and you want to show it...

Copy this code to a file, name it "localtime.php" and upload to your server:

Code: Select all

<?php
?>
<__script__ type="text/javascript">
<!--
function lclock() {
    document.write('<span id="lclock"></span>');
    // here's the PHP code that spawn out the server local time
    <?php list($sy,$sm,$sd,$sh,$si,$ss) = explode(",", date("Y,n,j,G,i,s")); 
    $sh = (intval($sh)-3);// change the offset here
    if (intval($sh)<12) {
    $sa = "am";
    } else {
    $sa = "pm";
    }
    if (intval($sh)>12) {
    $sh = (intval($sh)-12);
    }
    ?>
    // now we represent the server time as javascript date
    var server = new Date(<?=$sy?>, <?=$sm?>, <?=$sd?>, <?=$sh?>, <?=intval($si)?>, <?=intval($ss)?>).getTime();
    // calculate the client time
    var client = new Date().getTime();
    // run our ticker
    tick(client-server);
}
function tick(diff) {
    var d = new Date(new Date().getTime()-diff);
    var i = d.getMinutes(); if(i < 10) i='0'+i;
    var s = d.getSeconds(); if(s < 10) s='0'+s;
    var lclock = document.getElementById("lclock");
    if(lclock) {
        lclock.innerHTML='Local Time: '+
            d.getHours()+':'+i+':'+s+' <?=$sa?>';
        setTimeout('tick(' + diff + ');', 1000);
    }
}
//-->
</__script>
Note: You must edit the offset in this line to show the difference between your time and your server's time:

$sh = (intval($sh)-3);

For example...

If you are 4 hours behind your server, change it to:

$sh = (intval($sh)-4);

If you are 5 hours ahead of your server, change it to:

$sh = (intval($sh)+5);



Within the HEAD section of your template add:

Code: Select all

{php} require_once('localtime.php'); {/php}
Note: I uploaded the localtime.php file to the root directory. If you put it anywhere else, be sure to add the proper path in the above code.


Place this code where you want the clock to show - I put mine in the footer:

Code: Select all

{literal}<__script__ type="text/javascript">lclock();</__script>{/literal}

Nullig
carasmo
Power Poster
Power Poster
Posts: 506
Joined: Thu Feb 08, 2007 6:11 pm

Re: Display Server Time

Post by carasmo »

Can you get the day of the week in there:


Saturday, March 17, 2007


Thanks!
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Display Server Time

Post by calguy1000 »

If you have the customcontent module installed, it outputs many of these values as smarty variables.
try {get_template_vars}
which means you can do some funky logic on there to display different text for different days.
Like:  St. Patty's Day :)
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
SimonSchaufi

Re: Display Server Time

Post by SimonSchaufi »

The Javascript is not good for the guestbook for example where every post has its own date. To show the time in your local country it is better if you do that like this:
http://forum.cmsmadesimple.org/index.php/topic,1518.msg57674.html#msg57674

In the new CMS this funktion should be included!
lsandini

Re: Display Server Time

Post by lsandini »

Hello,

This would be great if I'd get it to work.

I copy-pasted the code and uploaded the servertime.php file to the root, made the modifications to the template, and applied the template to all pages and made it default, still I see no dynamic time wherever on my pages.

Is there any modification I need to make for this to work with 1.0.7 ?

Thanks for your help

Lorenzo
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm

Re: Display Server Time

Post by Nullig »

If you're using the servertime.php code (not the localtime.php), make sure you add this within the HEAD section of your template:

Code: Select all

{php} require_once('servertime.php'); {/php}
And place this code where you want the clock to show:

Code: Select all

{literal}<__script__ type="text/javascript">clock();</__script>{/literal}
Also, make sure you have this set in your config.php:

Code: Select all

$config['use_smarty_php_tags'] = true;
Nullig
lsandini

Re: Display Server Time

Post by lsandini »

Ach, the smarty tags were not enabled on this clean install.  :-[

Thanks for the nice code snippet and help !

Lorenzo
www.brokenbones.d2g.com
lsandini

Re: Display Server Time

Post by lsandini »

... I thought it was perfect, but a little thing still annoys me. The page is loaded, but with an error.

IE7 reports the following:
Line: 224
Char: 1
Error: Object expected
Code: 0
URL: www.brokenbones.d2g.com
On what file or template is the error to be looked for ?

In Firefox, it seems to load just fine.

Lorenzo
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm

Re: Display Server Time

Post by Nullig »

You are calling two scripts lclock and clock, but you only have the clock script loaded.

Look in the source code for the page and you'll see:

lclock();

placed before the footer, and:

clock();

placed within the footer.

The second one is the correct one for the script you have loaded in the HEAD section. The error is with the lclock script.

Nullig
lsandini

Re: Display Server Time

Post by lsandini »

:o  OMG, whay have I done ?

Very impressive debugging Nullig, I am speechless. How did you find it ?

Thanks again !
Lorenzo
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm

Re: Display Server Time

Post by Nullig »

Just looked at the page source, around the line # for the error.

Nullig
askme

Re: Display Server Time

Post by askme »

I put the following code into my site

Code: Select all

{php} require_once('http://www.erealmedia.com/cms102/servertime.php'); {/php}
and get following error at the top of the page....

require_once('http://www.erealmedia.com/cms102/servertime.php');
cyberman

Re: Display Server Time

Post by cyberman »

Have you insert it on text editor? If yes check html source view ...
Post Reply

Return to “Tips and Tricks”