Jump to content

[SOLVED] Need help on javascript server time


ILYAS415

Recommended Posts

Okay i got a javascript time which changes to match the time every __ seconds/minutes/hours. However this time depended on the persons computer time. I wanted the script to correspond to the server time so i made these changes...

 

Before:

<script>
<!--
function show2(){
if (!document.all&&!document.getElementById)
return
thelement=document.getElementById? document.getElementById("tick2"): document.all.tick2
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()
var dn="PM"
if (hours<12)
dn="AM"
if (hours>12)
hours=hours-12
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
var ctime=hours+":"+minutes+":"+seconds+" "+dn
thelement.innerHTML="Time: "+ctime+""
setTimeout("show2()",1000)
}
window.onload=show2
//-->
</script>

 

Server time:

<script>
<!--
function show2(){
if (!document.all&&!document.getElementById)
return
thelement=document.getElementById? document.getElementById("tick2"): document.all.tick2
var Digital=new Date()
var hours=Digital.setHours(<?php echo gmdate("h"); ?>);
var minutes=Digital.setMinutes(<?php echo gmdate("i"); ?>);
var seconds=Digital.setSeconds(<?php echo gmdate("s"); ?>);
var hour=Digital.getHours();
var minute=Digital.getMinutes();
var second=Digital.getSeconds();
var dn="PM"
if (hours<12)
dn="AM"
if (hours>12)
hours=hours-12
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
var ctime=hour+":"+minute+":"+second+" "+dn
thelement.innerHTML="Time: "+ctime+""
setTimeout("show2()",1000)
}
window.onload=show2
//-->
</script>

 

I have 2 problems, the first one is that:-

The server time doesnt change as the user stays on the same page

 

second problem:

Lets say the server time is 5am exact. This is how it appears on the site - 5:0:0 I want it to appear as 05:00:00

 

Please help its urgent.

 

p.s. if u may hav noticed i used php

var Digital=new Date()
var hours=Digital.setHours(<?php echo gmdate("h"); ?>);
var minutes=Digital.setMinutes(<?php echo gmdate("i"); ?>);
var seconds=Digital.setSeconds(<?php echo gmdate("s"); ?>);

you need to take those items out of the function and make global so that they are initalized only once to syncronize the times.

<script>
<!--
var Digital=new Date()
var hours=Digital.setHours(<?php echo gmdate("h"); ?>);
var minutes=Digital.setMinutes(<?php echo gmdate("i"); ?>);
var seconds=Digital.setSeconds(<?php echo gmdate("s"); ?>);
function show2(){
if (!document.all&&!document.getElementById)
return
// the rest of the code

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.