2012-04-18 70 views
0

我有一個倒計時從我的網頁倒計時從MySQL數據庫中的某個日期但是我不知道如何從數據庫中獲取日期到JavaScript上,我已經在網頁上回復了日期,只需要將它放入代碼並從那裏倒數即可。任何幫助都會很棒。由於我有一個JavaScript倒計時,但我需要從我的SQL數據庫抓取日期倒計時

這是我的javascript

function calcage(secs, num1, num2) { 
s = ((Math.floor(secs/num1))%num2).toString(); 
if (LeadingZero && s.length < 2) 
s = "0" + s; 
return "<b>" + s + "</b>"; 
} 


function CountBack(secs) { 
if (secs < 0) { 
document.getElementById("cntdwn").innerHTML = FinishMessage; 


return; 
} 
DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000)); 
DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24)); 
DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60)); 
DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60)); 

document.getElementById("cntdwn").innerHTML = DisplayStr; 
if (CountActive) 
setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod); 
} 

function putspan(backcolor, forecolor) { 
document.write("<span id='cntdwn' style='background-color:" + backcolor + 
      "; color:" + forecolor + "'></span>"); 
} 

if (typeof(BackColor)=="undefined") 
BackColor = "white"; 
if (typeof(ForeColor)=="undefined") 
ForeColor= "black"; 
if (typeof(TargetDate)=="undefined") 
TargetDate = "12/31/2020 5:00 AM"; 
if (typeof(DisplayFormat)=="undefined") 
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds."; 
if (typeof(CountActive)=="undefined") 
CountActive = true; 
if (typeof(FinishMessage)=="undefined") 
FinishMessage = ""; 
if (typeof(CountStepper)!="number") 
CountStepper = -1; 
if (typeof(LeadingZero)=="undefined") 
LeadingZero = true; 
CountStepper = Math.ceil(CountStepper); 
if (CountStepper == 0) 
CountActive = false; 
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990; 
putspan(BackColor, ForeColor); 
var dthen = new Date(TargetDate); 
var dnow = new Date(); 
if(CountStepper>0) 
ddiff = new Date(dnow-dthen); 
else 
ddiff = new Date(dthen-dnow); 
gsecs = Math.floor(ddiff.valueOf()/1000); 
CountBack(gsecs); 

這是我的代碼在HTML

<script language="JavaScript"> 
TargetDate = "4/17/2012 23:41"; 
BackColor = "white"; 
ForeColor = "black"; 
CountActive = true; 
CountStepper = -1; 
LeadingZero = true; 
DisplayFormat = "%%D%% Day, %%H%% Hour, %%M%% Minute, %%S%% Second "; 
FinishMessage = "Auction Ended"; 
</script> 
+0

所以你想從MySQL呼應日期並指定爲TargetDate? – Elen 2012-04-18 10:32:09

+0

是啊這是非常多,但生病需要它在DD/MM/YYYY HH:SS格式 – Jobbo05 2012-04-18 10:42:33

+0

你如何從數據庫中獲取查詢php代碼看起來像?所以我可以給你一個確切的答案。 – Elen 2012-04-18 10:51:44

回答

0

你的HTML文件應該是實際PHP中,你先打電話查詢:

$sql = "SELECT enddate FROM property WHERE propertyID = '1'"; 
$queryresult = mysql_query($sql) or die (mysql_error()); 
while ($row = mysql_fetch_assoc($queryresult)) 
{ $enddate=$row['enddate']; } 

然後你格式化你的日期爲:

$enddate = date("j/m/Y H:i",strtotime($enddate)); 

再經過PHP代碼你有你的HTML代碼和打印日期爲:

<script language="JavaScript"> 
TargetDate = "<?php echo $enddate; ?>"; 
BackColor = "white"; 
ForeColor = "black"; 
CountActive = true; 
CountStepper = -1; 
LeadingZero = true; 
DisplayFormat = "%%D%% Day, %%H%% Hour, %%M%% Minute, %%S%% Second "; 
FinishMessage = "Auction Ended"; 
</script>