创建与区域相关的时钟

创建与区域相关的时钟

问题描述:

I have a working clock for my local server and for my browser (local system). Both works great. Now what I want to do is find the time offset between the clocks, based on seconds, and add it to the local system clock.

For example: my system clock is: 10:00:00 and the server clock is 12:15:00 at the same date. then the offset will be: 02:15:00 (of course it will be in seconds but just for the example I wrote it that way). And then add it to (browser)local time so I eventually get to show the server time. 12:15:00.

This is what I'm doing here:

var interval = self.setInterval(function(){
  clock();
},1000);

function clock()
{   
    //Get local server time
    <?php $today = getdate(); ?>;
    var ser = <?php echo $today[0]; ?>;

    //Local client time
    var seconds = Date.now();
    var offset = seconds-ser*1000;
    parseInt(offset/1000);

    var d = new Date();
    d.setTime(Date.now()-offset);
    var h = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();

    if(s < 10)
      s = "0" + s;
    if(m < 10)
      m = "0" + m;
    if(h < 10)
      h = "0" + h;

    var tm = h+":"+m+":"+s;
    document.getElementById("clock").value = tm;
} 

But as soon as I put the variable

offset

in this line: d.setTime(Date.now()-offset);

The clock doesn't updates anymore. I guess there is an issue with PHP compatibility. Any idea how to solve it? Thanks

我的本​​地服务器和浏览器(本地系统)都有工作时钟。 两者都很棒。 我想要做的是根据秒数找到时钟之间的时间偏移,并将其添加到本地系统时钟。 p>

例如:我的系统时钟是:10:00:00,服务器时钟是同一天的12:15:00。 偏移量为:02:15: 00(当然它将在几秒钟内,但仅仅是我用这种方式编写的例子)。 然后将其添加到(浏览器)本地时间,以便最终显示服务器时间。 12时15分零零秒。 p>

这就是我在这里所做的: p>

  var interval = self.setInterval(function(){
 clock()  ; 
},1000); 
 
function clock()
 {
 //获取本地服务器时间
&lt;?php $ today = getdate();  ?&gt ;; 
 var ser =&lt;?php echo $ today [0];  ?&gt ;; 
 
 //本地客户端时间
 var seconds = Date.now(); 
 var offset = seconds-ser * 1000; 
 parseInt(offset / 1000); 
 
 var d  = new Date(); 
 d.setTime(Date.now() -  offset); 
 var h = d.getHours(); 
 var m = d.getMinutes(); 
 var s = d。  getSeconds(); 
 
 if if(s&lt; 10)
s =“0”+ s; 
 if(m&lt; 10)
m =“0”+ m; 
 if(h&lt; 10  )
h =“0”+ h; 
 
 var tm = h +“:”+ m +“:”+ s; 
 document.getElementById(“clock”)。value = tm; 
} 
   pre> 
 
 

但是只要我把变量 p>

  offset 
  code>  pre> 
 \  n 

在这一行: d.setTime(Date.now() - offset); code> p>

时钟不再更新。 我想PHP兼容性存在问题。 任何想法如何解决它? 谢谢 p> div>

The offset calculation has to be outside of the repeadly called function:

//Get local server time
<?php $today = getdate(); ?>;
var ser = <?php echo $today[0]; ?>;

//Local client time
var seconds = Date.now();
var offset = seconds-ser*1000;
parseInt(offset/1000); // BY THE WAY: As the returnval of parseInt isn't stored anywhere, this line does nothing

var interval = self.setInterval(function(){
  clock();
},1000);

function clock()
{   

    var d = new Date();
    d.setTime(Date.now()-offset);
    var h = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();

    if(s < 10)
      s = "0" + s;
    if(m < 10)
      m = "0" + m;
    if(h < 10)
      h = "0" + h;

    var tm = h+":"+m+":"+s;
    document.getElementById("clock").value = tm;
}

Perhaps you're missing something with "parseInt"?

parseInt requires a variable to store the value.

So, if you write:

var offset = seconds-ser*1000;
    parseInt(offset/1000);

Is like if you write:

var offset = seconds-ser*1000;

So, replace:

    var offset = seconds-ser*1000;
    parseInt(offset/1000);

with this:

var offset = seconds-ser*1000;
offset = parseInt(offset/1000);

Tried on my browser and works.

The whole script should look like this (I used console.log instead):

var interval = self.setInterval(function(){
              clock();
            },1000);

            function clock()
            {   
                //Get local server time
                <?php $today = getdate(); ?>;
                var ser = new Date(<?php echo $today[0]; ?>);
                var seconds = Date.now();
                var offset = seconds-ser*1000;
                offset = parseInt(offset/1000);

                //Local client time


                var d = new Date();
                d.setTime(Date.now()-offset);
                var h = d.getHours();
                var m = d.getMinutes();
                var s = d.getSeconds();

                if(s < 10)
                  s = "0" + s;
                if(m < 10)
                  m = "0" + m;
                if(h < 10)
                  h = "0" + h;

                var tm = h+":"+m+":"+s;
                console.log(tm);
            } 

Just parse your console.log where it needs to be parsed.