2013-06-29 82 views
0

我有一個用於倒計時的JavaScript代碼。我有一個日期,我從數據庫通過PHP收集,我需要通過日期對象的JavaScript函數,但我不知道如何。我試過這個:將參數傳遞給來自php的javascript onload事件

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

但它似乎沒有工作。這裏是我有

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(); 
} 
+0

你是否看到任何錯誤(檢查js控制檯)。你也可以給出一個php插入日期的例子 – wgcrouch

回答

0

您可以通過多種方式的javascript日期對象的js代碼:

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); 

所以,選項之一可能是:

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

,並在您的功能開始:

var date = new Date(date); 

只有當你的$ event-> startDate是有效的PHP日期格式時才能工作。你可以瞭解更多關於HERE

相關問題