【问题标题】:Trying to use jQuery Countdown with UNIX timestamp from php尝试使用 jQuery Countdown 和来自 php 的 UNIX 时间戳
【发布时间】:2012-05-08 03:23:07
【问题描述】:

正如标题所说,我在显示结束日期的右左时间时遇到了问题。

我正在使用这个 jquery 插件:http://keith-wood.name/countdown.html

假设我们的时间戳是由这样的 PHP 脚本生成的:$end_date = strtotime($row4['end']); // 2012-05-09 06:00:00 becomes 1336536000

我使用这个($i 在循环中使用):

$('.count-<?php echo $i; ?>').countdown({until: new Date(<?php echo $end_date; ?> * 1000), layout: '{dn} days {hnn} hrs {mnn} min {snn} sec', compact: true});

现在是上午 06:21。我得到的是“1 天 17 小时 04 分 21 秒”,而不是预期的 1 天结果。

我在这里错过了什么?

【问题讨论】:

  • $i 是一个通过 while 循环增加的变量。 $i = 0; while() { $i++; } 我使用它是因为我想显示许多项目的倒计时。
  • 我不想详细介绍倒计时 api,但您确定您使用的是同一个时区吗?最好是世界各地的格林尼治标准时间。
  • 好的,现在我已将 PHP 和 jquery 倒计时时区设置为 GMT 0,但 PHP 倒计时和 jquery 倒计时结果之间仍然存在差异。 PHP倒计时结果是1天,还剩1小时,jquery结果是1天20小时,直到2012-05-09 06:00:00。

标签: php javascript jquery countdown


【解决方案1】:

我已经使用 API 有一段时间了,但我似乎无法弄清楚你的代码是怎么回事。也许(但也许)你使用了 untill 属性错误。

我常用的代码:

$(function(){
    var countdown = $('#countdown'),
        ts = new Date(<?php echo $date_to * 1000; ?>),
        finished = true;

    if((new Date()) > ts)
    {
        finished = false;
    }

    $('#cool-countdown').countdown({
        timestamp   : ts,
        callback    : function(days, hours, minutes, seconds)
        {
            var message = "";

            message += days + " days, ";
            message += hours + " hours, ";
            message += minutes + " minutes, ";
            message += seconds + " seconds ";

            message = (finished ? "Countdown finished" : "left untill the New Year");

            countdown.html(message);
        }
    });

});

显然你应该将其扩展为单数。

【讨论】:

  • 您好,感谢您的回复。抱歉,我的问题不在于布局。我认为生成的结果是错误的。我在将 UNIX 时间戳转换为 javascript 日期对象时做错了吗?
  • @ggirtsou 也许尝试在服务器端乘以 $end_date。例如。 - ...Date(&lt;?php echo ($end_date * 1000); ?&gt;)...
  • OK 试过了,但结束日期 2012-05-09 06:00:00 仍然有 1 天 20 小时
  • @ggirtsou 更新了我的解释。请仔细阅读并测试它是否适合您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-14
  • 1970-01-01
  • 2013-04-18
  • 2011-03-23
相关资源
最近更新 更多