Hi,
Not really a php question but it includes php, so I thought somebody might be able to help.
I've got a javascript countdown script that gets a future time from a mysql database in UNIX_TIMESTAMP format using php.
It then counts down how long there is to go until this time. The problem is, it gets the current time from the users machine instead of the server. This means that it will cause problems if the user does not have the correct time set or they are in a different time zone.
Having the exact current time is a mission-critical part of the application I am creating. Here is the code:-
<head>
<?
$end_query = "SELECT UNIX_TIMESTAMP(auction_end) FROM item WHERE id = $id";
$end_result = mysql_query($end_query, $mysql_access);
list($date) = mysql_fetch_array($end_result);
$year = date(Y, $date);
$month = date(n, $date);
$day = date(j, $date);
$hour = date(G, $date);
$minute = date(i, $date);
$second = date(s, $date);
echo "<script language='javascript'>";
echo "Year=".$year."\n";
echo "Month=".$month."\n";
echo "Day=".$day."\n";
echo "Hour=".$hour."\n";
echo "Minute=".$minute."\n";
echo "Second=".$second."\n";
echo "</script>";
?>
<script type="text/javascript" language="JavaScript">
function setcountdown(theyear,themonth,theday,thehour,themin,thesec){
yr=theyear;mo=themonth;da=theday;hr=thehour;min=themin;sec=thesec
}
setcountdown(Year,Month,Day,Hour,Minute,Second)
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var crosscount=''
function countdown(){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
futurestring=montharray[mo-1]+" "+da+", "+yr+" "+hr+":"+min+":"+sec
dd=Date.parse(futurestring)-Date.parse(todaystring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
if (dday!=1 && dday!=0) {
var SufDays=" days, "}
else if (dday<=0) {
var SufDays=""
var dday=""}
else {var SufDays=" day, "}
if (dhour!=1 && dhour!=0) {
var SufHour=" hours, "}
else if (dhour<=0) {
var SufHour=""
var dhour=""}
else {var SufHour=" hour, "}
if (dmin!=1 && dmin!=0) {
var SufMin=" minutes and "}
else if (dmin<=0) {
var SufMin=" and "
var dmin=""}
else {var SufMin=" minute and "}
if (dsec!=1) {
var SufSecs=" seconds"}
else {var SufSecs=" second"}
if (dday<=-1){
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write("Auction has ended")
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML="Auction has ended"
return
}
else{
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(dday + SufDays + dhour + SufHour + dmin + SufMin + dsec + SufSecs)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=dday + SufDays + dhour + SufHour + dmin + SufMin + dsec + SufSecs
}
setTimeout("countdown()",1000)
}
</script>
</head>
<body onload="start_countdown()";>
<script type="text/javascript" language='javascript'>
function start_countdown(){
if (document.layers)
document.countdownnsmain.visibility="show"
else if (document.all||document.getElementById)
crosscount=document.getElementById&&!document.all?document.getElementById("countdownie") : countdownie
countdown()
}
if (document.all||document.getElementById)
document.write('<span id="countdownie" width: 300px; height: 12px;"></span>')
</script>
</body>
Thanks,