dongmei425373 2014-11-05 07:03
浏览 121
已采纳

PHP时间到Javascript时钟

I want to simply just pull the time from the server. And continue it ticking with javascript.

My 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 

My 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!!???
});

I normally would just use the users local computer time, but each server is in a different location, and when they are visiting this page, I want the clock to tick to the server they're currently visiting.

  • 写回答

2条回答 默认 最新

  • doulian8485 2014-11-05 20:47
    关注

    $time is the number of seconds since unix epoch, expressed as milliseconds. $offset is the number of hours difference from GMT*, Eg '-0800'. Your first problem is that you are adding the offset to the timestamp. At most, you are changing the time by 1.4 seconds, where you seem to intend to change the time by hours.

    It would be much easier if you use a DateTime object instead. Then you can call .getOffset() which gives you the difference in seconds:

    <?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;
    ?>
    

    So far, we've been dealing only with UTC dates. Your other problem is that you are using the local date methods to get values out. You should use the UTC versions of these methods.

    //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);
    

    Here's the whole thing, with simulated server time:

    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>


    *Or, so says the documentation. Last week, my machine said -0700, which is the octal representation of -448. My time is neither 700 hours earlier than GMT, nor is it 448 hours earlier than GMT, it's 7 hours earlier. It annoys me that the documentation states that it is the time offset in hours, when really it's the time offset in ISO 8601 Format.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口