【问题标题】:Javascript countdown using server-side time to completeJavascript倒计时使用服务器端时间完成
【发布时间】:2014-07-03 16:15:10
【问题描述】:

我正在使用这个脚本来倒计时,它可以工作。

            <script type="text/javascript">

        (function (e) {
        e.fn.countdown = function (t, n) {
        function i() {
        eventDate = Date.parse(r.date) / 1e3;
        currentDate = Math.floor(e.now() / 1e3);
        if (eventDate <= currentDate) {
        n.call(this);
        clearInterval(interval)
        }
        seconds = eventDate - currentDate;
        days = Math.floor(seconds / 86400);
        seconds -= days * 60 * 60 * 24;
        hours = Math.floor(seconds / 3600);
        seconds -= hours * 60 * 60;
        minutes = Math.floor(seconds / 60);
        seconds -= minutes * 60;
        days == 1 ? thisEl.find(".timeRefDays").text("day") : thisEl.find(".timeRefDays").text("day");
        hours == 1 ? thisEl.find(".timeRefHours").text("hours") : thisEl.find(".timeRefHours").text("hours");
        minutes == 1 ? thisEl.find(".timeRefMinutes").text("Minutes") : thisEl.find(".timeRefMinutes").text("Minutes");
        seconds == 1 ? thisEl.find(".timeRefSeconds").text("Seconds") : thisEl.find(".timeRefSeconds").text("Seconds");
        if (r["format"] == "on") {
        days = String(days).length >= 2 ? days : "0" + days;
        hours = String(hours).length >= 2 ? hours : "0" + hours;
        minutes = String(minutes).length >= 2 ? minutes : "0" + minutes;
        seconds = String(seconds).length >= 2 ? seconds : "0" + seconds
        }
        if (!isNaN(eventDate)) {
        thisEl.find(".days").text(days);
        thisEl.find(".hours").text(hours);
        thisEl.find(".minutes").text(minutes);
        thisEl.find(".seconds").text(seconds)
        } else {
        alert("Invalid date. Example: 30 Tuesday 2013 15:50:00");
        clearInterval(interval)
        }
        }
        thisEl = e(this);
        var r = {
        date: null,
        format: null
        };
        t && e.extend(r, t);
        i();
        interval = setInterval(i, 1e3)
        }
        })(jQuery);
        $(document).ready(function () {
        function e() {
        var e = new Date;
        e.setDate(e.getDate() + 60);
        dd = e.getDate();
        mm = e.getMonth() + 1;
        y = e.getFullYear();
        futureFormattedDate = mm + "/" + dd + "/" + y;
        return futureFormattedDate
        }
        $("#countdown").countdown({
        date: "<?php echo $newcounter ?> ", // Change this to your desired date to countdown to
        format: "on"
        });
        });

        </script> 

此脚本使用我的客户端日期,但我想使用我的服务器日期。如何读取从我的服务器读取的日期?我在我的脚本中尝试了这段代码:

currentDate = <?php echo time() ?>;

但是我的倒计时停止并且不起作用。

【问题讨论】:

  • Javascript 运行客户端,所以如果你想要服务器时间,你需要对服务器进行 AJAX 调用。
  • 谢谢你怎么能调用whit ajax?
  • 您在$newcounter 上使用的当前值是多少?
  • @APAD1,不需要AJAX...他只是想在创建页面时获取服务器时间...

标签: javascript php date


【解决方案1】:

你有服务器时间。您正在正确地进行此操作 - 无需使用 AJAX。我认为您的问题涉及您传递给倒计时功能的日期格式。倒计时似乎需要一个数字(newcounter = 要等待的毫秒数?),但您传递的是时间戳(&lt;?php echo time() ?&gt;)。

参见上面的@APAD1 评论。

【讨论】:

    【解决方案2】:
    <?php
        date_default_timezone_set('Europe/Vienna');
        $now = new DateTime();
        $dateJsFormat = $now->format('Y') .',' . ($now->format('m')-1) .','.$now->format('d') .',' . $now->format('H') .','.$now->format('i');
    ?>
    
    <script>
    
    
      var date = new Date(<?= $dateJsFormat ?>);
      alert(date);
    
    </script>
    

    JS-Date 对象期望这种格式作为参数:

    new Date(Year, Month, day, hour, minute) // JS-Code
    

    月份必须减1:

    $now->format('m')-1 // see php-code above
    

    在你的例子中,你必须设置这个:

    $("#countdown").countdown({
        date: "<?= dateJsFormat ?>", // Change this to your desired date to countdown to
        format: "on"
    });
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-10-09
      • 2016-04-04
      • 1970-01-01
      • 2013-07-17
      • 2023-03-11
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      相关资源
      最近更新 更多