drj58429 2012-10-09 04:19
浏览 49
已采纳

PHP时区问题

I am using the countdown here http://keith-wood.name/countdown.html and I have added the server time and everything but now with daylight savings my application adds an extra hour to all countdown times at http://www.artfido.com

I have tried changing the server time to not update with daylight savings but my countdown still has an hour added.

UTC time is +10 but don't know how to change to not have the additional hour.

Code sample

server-time.php  
<?php 
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
header("Expires: Fri, 1 Jan 2010 00:00:00 GMT"); // Date in the past 
header("Content-Type: text/plain; charset=utf-8"); // MIME type 
$now = new DateTime();
echo $now->format("M j, Y H:i:s O")."
";
?>

Countdown script
$('#defaultCountdown').countdown({
until: endAuction,
serverSync: serverTime,
timezone: +10,
format: 'DHMS',...

function serverTime() { 
var time = null; 
$.ajax({url: 'server-time.php?random=' + Math.floor(Math.random() * 1000000), 
 async: false, dataType: 'text', 
 success: function(text) { 
 time = new Date(text); 
}, error: function(http, message, exc) { 
time = new Date(); 
}}); 
return time; 
}
  • 写回答

1条回答 默认 最新

  • doukanhua0752 2012-10-09 04:27
    关注

    You have used code this way:

    function serverTime() {
        var time = null;
        $.ajax({url: 'cgi/server-time.php?random=' + Math.floor(Math.random() * 1000000),
            async: false, dataType: 'text',
            success: function(text) {
                time = new Date(text);
            }, error: function(http, message, exc) {
                time = new Date();
        }});
        return time;
    }
    

    This function, doesn't return anything because the AJAX Call is asynchronous. One good way, is to do it in the success function. I meant the initialization of the timer.

    Because, when you call this serverTime() function, it always returns null. It is not instant. Do one thing. Have a function called, startTimer(time) and pass the server's time in the time variable to the startTimer() and inside the function, define this way:

    function startTimer(time)
    {
        var endAuction = "";
        $(function () {
            endAuction = new Date(
                2012,
                10,
                08,
                12,
                24,
                26                                                                );
            $('#defaultCountdown8077').countdown('destroy')
            $('#defaultCountdown8077').countdown({
                until: endAuction,
                serverSync: time,
                timezone: +10,
                format: 'DHMS',
                expiryUrl: 'http://www.artfido.com'
            });
        });
    }
    

    Also the serverTime(); you are calling somewhere in the middle, and in no way, it would be useful.

    Your JavaScript code would be:

    function initTimer() {
        var time = null;
        $.ajax({url: 'cgi/server-time.php?random=' + Math.floor(Math.random() * 1000000),
            async: false, dataType: 'text',
            success: function(text) {
                time = new Date(text);
                startTimer(time);
            }
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?