drvlf9739 2019-01-08 07:09
浏览 66

如何在javascript中获取服务器时间

I am using jquery.countdown.js for time calculating If i change the time on local pc the counter will change I want to get the time from server and compare it with the time saved in Database. Jquery code is given below and the anonymous function called is also given below.

$('#paper_countdown').countdown({
    timestamp   : (new Date($('.intime_time').val())),

    callback    : function(days, hours, minutes, seconds){

        var message = "";
        message += days + " day" + ( days==1 ? '':'s' ) + ", ";
        message += hours + " hour" + ( hours==1 ? '':'s' ) + ", ";
        message += minutes + " minute" + ( minutes==1 ? '':'s' ) + " and ";
        message += seconds + " second" + ( seconds==1 ? '':'s' ) + " <br />";

        if(newYear){
            message += "left until the new year!";
        }
        else {
            message += "left to 10 days from now!";
        }

        note.html(message);
    }
});

This is the anonymous function from jquery.countdown.js

var days = 24 * 60 * 60,
        hours = 60 * 60,
        minutes = 60;

// Creating the plugin
$.fn.countdown = function (prop) { 
    var options = $.extend({
        callback: function () {
        },
        timestamp: 0
    }, prop);

    var left, d, h, m, s, positions;

    // Initialize the plugin
    init(this, options);

positions = this.find('.position');
    (function tick() {

        left = Math.floor((options.timestamp - (new Date())) / 1000);
        if (left < 0) {
            left = 0;
        }

        // Number of hours left
        h = Math.floor(left / hours);
        updateDuo(2, 3, h);
        left -= h * hours;

        // Number of minutes left
        m = Math.floor(left / minutes);
        updateDuo(4, 5, m);
        left -= m * minutes;

        // Number of seconds left
        s = left;
        updateDuo(6, 7, s);
        // Calling an optional user supplied callback
        options.callback(h, m, s);

        // Scheduling another call of this function in 1s
        if (h == 0 && m == 0 && s == 0)
            location.reload();
         else
            setTimeout(tick, 1000);
    })();

    // This function updates two digit positions at once
    function updateDuo(minor, major, value) {
        switchDigit(positions.eq(minor), Math.floor(value / 10) % 10);
        switchDigit(positions.eq(major), value % 10);
    }
    return this;
};

My Php code to calculate the remaining time is

public function remainingTime($exam_id, $sub_id,$std_reg_detail_id){
    $papers = \backend\models\Papers::find()->where('exam_id =:eid AND id =:sid',[':eid'=>$exam_id, ':sid'=>$sub_id])->one();
    $std_reg_detail = \backend\models\StudentRegDetail::find()->where(['paper_id'=>$sub_id,'reg_exam_id'=>$exam_id,'id'=>$std_reg_detail_id])->one();
    $rem_time = 0;
    if($papers != NULL){
        $time = $std_reg_detail['paper_date'].' '.$std_reg_detail['paper_time'];
        $total_time = $papers['total_time_allowed'];
        $dt = new \DateTime($time);
        $start_time = $dt->format('Y-m-d H:i:s');   
        $dt->add(new \DateInterval('PT' . $total_time . 'M'));
        $end_time = $dt->format('d M Y H:i:s');
        $rem_time = $end_time;
        // dd($rem_time);
    }
    return $rem_time;
}

To display in the view

<div class="clock">
        <input type="hidden" value="<?php echo $rem_time ?>" class="intime_time"/>
        <h3>Remaining Time</h3>
        <div id="paper_countdown"></div>
    </div> 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计