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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.