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);
            }
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法