2

我的脚本在 Chrome 中没问题,但在 FireFox 和 IE 中结果是:NaN:NaN。这是我的脚本:有人知道出了什么问题吗?

$db[time] 的输出是:2013-10-07 14:28:35(时间戳数据库)

<script>
var end = new Date('<?=$db[time]?>');

    var _second = 1000;
    var _minute = _second * 60;
    var _hour = _minute * 60;
    var _day = _hour * 24;
    var timer;

    function showRemaining() {
        var now = new Date();
        var distance = end - now;
        if (distance < 0) {

            clearInterval(timer);
            document.getElementById('countdown').innerHTML = 'ITS NOW TIME!</font><BR><BR>';                
            return;
        }
        var days = Math.floor(distance / _day);
        var hours = Math.floor((distance % _day) / _hour);
        var minutes = Math.floor((distance % _hour) / _minute);
        var seconds = Math.floor((distance % _minute) / _second);


        document.getElementById('countdown').innerHTML = '<font color="orange">' + minutes + ':';
        document.getElementById('countdown').innerHTML += '<font color="orange">' + seconds + ' minutes</font>';
    }

    timer = setInterval(showRemaining, 1000);
</script>
4

3 回答 3

2

这是因为 的值$db[time]

您在评论中说日期/时间的格式是2013-10-07 14:28:35MySQL 的默认 DATETIME 输出,当它需要时2013-10-07T14:28:35

Chrome 支持这种2013-10-07 14:28:35格式是为了方便,但它不在 Javascript 规范中,因此并非所有其他浏览器都支持。

尝试这个:

<? echo str_replace(' ', 'T', $db['time']); ?>
于 2013-10-07T12:58:41.113 回答
2

javascript 时间格式与 php 不同,因此 firefox 将显示 NaN:NaN:NaN。你应该隐藏时间如下:

PHP日期格式

$yourDate = date("M d, Y H:i:s", strtotime($db['time'])));

得到这样的时间

date = new date("<?=$yourDate?>").getTime();
于 2017-07-13T08:05:34.640 回答
0

您可以自己解析日期字符串,而不是依赖具有现代方法的现代浏览器,如下所示应该是跨浏览器友好的。倒计时以毫秒为单位,您可以根据需要对其进行格式化。

HTML

<div id="countdown"></div>

Javascript

function isGregorianLeapYear(year) {
    return year % 400 === 0 || year % 100 !== 0 && year % 4 === 0;
}

function daysInGregorianMonth(year, month) {
    var days;

    if (month == 2) {
        days = 28;
        if (isGregorianLeapYear(year)) {
            days += 1;
        }
    } else {
        days = 31 - ((month - 1) % 7 % 2);
    }

    return days;
}

function timeStampToDate(timeStamp) {
    var dateObject,
        dateTime,
        date,
        time,
        value;

    if (typeof timeStamp === "string") {
        dateTime = timeStamp.split(" ");
        if (dateTime.length === 2) {
            date = dateTime[0].split("-");
            if (date.length === 3) {
                value = +date[0];
                if (date[0].length === 4 && value >= -9999 && value <= 9999) {
                    value = +date[1];
                    if (date[1].length === 2 && value >= 1 && value <= 12) {
                        date[1] = value - 1;
                        value = +date[2];
                        if (date[2].length === 2 && value >= 1 && value <= daysInGregorianMonth(+date[0], date[1])) {
                            time = dateTime[1].split(":");
                            if (time.length === 3) {
                                value = +time[0];
                                if (time[0].length === 2 && value >= 0 && value <= 23) {
                                    value = +time[1];
                                    if (time[1].length === 2 && value >= 0 && value <= 59) {
                                        value = +time[2];
                                        if (time[2].length === 2 && value >= 0 && value <= 59) {
                                            dateObject = new Date(Date.UTC.apply(Date, date.concat(time)));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if (typeof dateObject === "undefined") {
        dateObject = new Date(NaN);
    }

    return dateObject;
}

function emptyNode(node) {
    while (node.firstChild) {
        node.removeChild(node.firstChild);
    }
}

var countdown = document.getElementById("countdown"),
    end = timeStampToDate("2013-10-07 16:57:00"),
    intervalId;

end = end.getTime() + end.getTimezoneOffset() * 60000;

function showRemaining() {
    var now = new Date().getTime();

    if (isNaN(end) || isNaN(now)) {
        clearInterval(intervalId);
        emptyNode(countdown);
        countdown.appendChild(document.createTextNode("Error"));
    } else if (now < end) {
        emptyNode(countdown);
        countdown.appendChild(document.createTextNode(end - now));
    } else {
        clearInterval(intervalId);
        emptyNode(countdown);
        countdown.appendChild(document.createTextNode("ITS NOW TIME!"));
    }
}

intervalId = setInterval(showRemaining, 1000);

jsFiddle

于 2013-10-07T15:01:43.087 回答