PHP时间到Javascript时钟

时间:2014-11-05 07:03:59

标签: javascript php time timezone clock

我想简单地从服务器上抽出时间。 并继续使用javascript打勾。

我的PHP。

//Get current UNIX timestamp (no offset, just straight timestamp)
$time = time()*1000;

//Get offset of server time
$offset = date('O');

// Convert into a new timestamp based on the timezone
$newtime = $time+$offset;

// a vardump($newtime) here gives me
// 1415169764400 

我的Javascript ...

$(document).ready(function(){

// Here Im putting the server time into a variable
var serverTime = "<?php echo $newtime;?>"; 
//console.log(serverTime) gives me 1415169764400

// Local computer time into a variable
var localTime = new Date().getTime(); 
//console.log(localTime) here gives me 1415170692954

// Offset between the computer and the server
var timeDiff = serverTime - localTime; 
// console.log(timeDiff) here gives me -928554  

//The ticking clock function
setInterval(function () {
    // Set clock to Computer time plus the time difference
    var today = new Date(Date.now()+timeDiff);
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    m = checkTime(m); //checktime() is just a leading zero function
    s = checkTime(s); // checktime() is just a leading zero function
    var formatted = h+":"+m+":"+s;
    $('.serverTime').html(formatted);
}, 2000);
//PROBLEM IS THAT THIS DISPLAYS THE CURRENT TIME ON MY COMPUTER
// NO MATTER WHAT I TRY, IT ALWAYS JUST DISPLAYS THE LOCAL TIME
// No matter what PHP timezone I put.
// The time Diff and server times are working properly.
// Any ideas whats happening!!???
});

我通常只会使用用户本地计算机时间,但每个服务器位于不同的位置,当他们访问此页面时,我希望时钟勾选他们当前正在访问的服务器。

2 个答案:

答案 0 :(得分:1)

$time是自unix纪元以来的秒数,表示为毫秒$offset是与GMT *的差异小时的数量,例如'-0800'。您的第一个问题是您要将偏移量添加到时间戳。您最多将时间更改为1.4秒,您似乎打算将时间更改为小时。

如果您使用DateTime对象会更容易。然后你可以拨打.getOffset(),它会在几秒钟内给你带来差异:

<?php 
$date = new DateTime();

// Get offset for UNIX timestamp based on server timezone
$time = $date->getTimestamp();

//Get offset of server time
$offset = $date->getOffset();

// Convert into a new timestamp based on the timezone
$newtime = ($time + $offset) * 1000;
?>

到目前为止,我们一直只处理UTC日期。您的另一个问题是您使用本地日期方法来获取值。您应该使用这些方法的UTC版本。

//Actual ticking clock
setInterval(function () {
    var today = new Date(Date.now() + timeDiff);
    var h=today.getUTCHours();
    var m=today.getUTCMinutes();
    var s=today.getUTCSeconds();
    m = checkTime(m);
    s = checkTime(s);
    var formatted = h+":"+m+":"+s;
    $('.serverTime').html(formatted);
}, 2000);

这是整个事情,模拟服务器时间:

function simulateServerTime() {
    var time = new Date();
    // Add 1 minute and 12 seconds to simulate the clocks being a little out of sync
    time.setMinutes(time.getMinutes() + 1, 12);
    // Reduce to seconds to simulate php
    time = Math.round(time / 1000);
    // Specify an offset of -6 hours
    var offset = -6 * 60 * 60;
    return (time + offset) * 1000;
}

var serverTime = simulateServerTime();

var localTime = new Date();

// Offset between the computer and the server
var timeDiff = serverTime - localTime;

// The ticking clock function
setInterval(function () {
  var today = new Date(Date.now() + timeDiff);
  var h=today.getUTCHours();
  var m=today.getUTCMinutes();
  var s=today.getUTCSeconds();
  m = checkTime(m);
  s = checkTime(s);
  var formatted = h+":"+m+":"+s;
  document.querySelector(".serverTime").innerHTML = formatted;
}, 1000);

// Helper function for leading 0's
function checkTime(i) {
  if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
  return i;
}
<div class="serverTime"></div>


*或者,so says the documentation。上周,我的机器说-0700,这是-448的八进制表示。我的时间比格林威治标准时间早700小时,也不比格林威治标准时间提前448小时,早7小时。令我很恼火的是,文档说明它是以小时为单位的时间偏移,实际上是time offset in ISO 8601 Format

答案 1 :(得分:0)

时间戳不包括时区偏移量。每个人的unix时间都是1970年1月1日UTC的偏差

如果要在客户端显示服务器时区,则PHP必须提供服务器时区。关于客户时区,您可以参考this

此外,我喜欢使用moment.js来处理日期/时间问题。


我刚刚使用片刻修改了客户端javascript。希望你能得到它所需要的一切

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment.min.js"></script>
<?php

date_default_timezone_set("Europe/Athens");

$serverTime =  time() * 1000 ;
$timezone = date('O');

?>
<div id="result"></div>
<script>
  var serverTime = <?php echo $serverTime;?>,
    timezone = "<?php echo $timezone;?>",
    timeDiff = serverTime - Date.now();

  setInterval(function () {
      result.innerHTML= moment().add(timeDiff).zone(timezone).format('HH:mm:ss Z') + "<br/>" + result.innerHTML  ;
  }, 2000);
</script>