Page 1 of 2
Display Server Time
Posted: Fri Mar 02, 2007 4:50 pm
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
Re: Display Server Time
Posted: Fri Mar 02, 2007 6:39 pm
by cyberman
Thanx for that nice gimmick

Re: Display Server Time
Posted: Fri Mar 02, 2007 9:14 pm
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
Re: Display Server Time
Posted: Sat Mar 17, 2007 7:18 pm
by carasmo
Can you get the day of the week in there:
Saturday, March 17, 2007
Thanks!
Re: Display Server Time
Posted: Sat Mar 17, 2007 10:12 pm
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 
Re: Display Server Time
Posted: Tue Apr 24, 2007 10:21 am
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!
Re: Display Server Time
Posted: Mon Jun 18, 2007 8:55 pm
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
Re: Display Server Time
Posted: Mon Jun 18, 2007 9:06 pm
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
Re: Display Server Time
Posted: Tue Jun 19, 2007 5:59 am
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
Re: Display Server Time
Posted: Tue Jun 19, 2007 1:33 pm
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:
On what file or template is the error to be looked for ?
In Firefox, it seems to load just fine.
Lorenzo
Re: Display Server Time
Posted: Tue Jun 19, 2007 3:01 pm
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
Re: Display Server Time
Posted: Tue Jun 19, 2007 5:01 pm
by lsandini

OMG, whay have I done ?
Very impressive debugging Nullig, I am speechless. How did you find it ?
Thanks again !
Lorenzo
Re: Display Server Time
Posted: Tue Jun 19, 2007 5:11 pm
by Nullig
Just looked at the page source, around the line # for the error.
Nullig
Re: Display Server Time
Posted: Mon Jul 02, 2007 7:56 am
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');
Re: Display Server Time
Posted: Mon Jul 02, 2007 10:02 am
by cyberman
Have you insert it on text editor? If yes check html source view ...