0

カウントダウン用のJavaScriptコードがあります。私はデータベース usu php から収集した日付を持っています。日付オブジェクトを JavaScript 関数に渡す必要がありますが、方法がわかりません。私はこれを試しました:

<body onload="countIt(<?php echo $event->startDate;?>)">

しかし、うまくいかないようです。ここに私が持っているjsコードがあります

function countIt(date) {
    ///i will parse the date into below variables if I can get the date.
    year = 2013;
    month = 09;
    day = 28;
    hours = 12;
    minutes = 00;
    seconds = 00;

    setTimeout(function() {
        endDate = new Date(year, month, day, hours, minutes, seconds, 00);
        thisDate = new Date();
        thisDate = new Date(thisDate.getFullYear(), thisDate.getMonth() + 1, thisDate.getDay(), thisDate.getHours(), thisDate.getMinutes(), thisDate.getSeconds(), 00, 00);

        var daysLeft = parseInt((endDate - thisDate) / 86400000);
        var hoursLeft = parseInt((endDate - thisDate) / 3600000);
        var minutsLeft = parseInt((endDate - thisDate) / 60000);
        var secondsLeft = parseInt((endDate - thisDate) / 1000);

        seconds = minutsLeft * 60;
        seconds = secondsLeft - seconds;

        minutes = hoursLeft * 60;
        minutes = minutsLeft - minutes;

        hours = daysLeft * 24;
        hours = (hoursLeft - hours) < 0 ? 0 : hoursLeft - hours;

        days = daysLeft;

        startCount(days, hours, minutes, seconds);
    }, 1000);
}

function startCount(days, hours, minutes, seconds) {
    document.getElementById("counter").innerHTML = "DAYS " + days + ", HOURS " + hours + ", MINUTES " + minutes + ", SECONDS: " + seconds;
    countIt();
}
4

1 に答える 1

0

複数の方法で JavaScript の日付オブジェクトを作成できます。

    var d = new Date();
    var d = new Date(milliseconds);
    var d = new Date(dateString);
    var d = new Date(year, month, day, hours, minutes, seconds, milliseconds); 

したがって、オプションの1つは次のようになります。

    <body onload="countIt(<?php echo strtotime($event->startDate);?>)">

そしてあなたの機能の始めに:

    var date = new Date(date);

これは、 $event->startDate が有効な PHP 日付形式である場合にのみ機能します。詳細については、こちらをご覧ください。

于 2013-06-30T00:35:22.557 に答える