2012-04-18 33 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>