Forum Moderators: open
timeLeft1.setHours(h,m,s);
var serverTime = '<?php print date("F d, Y H:i:s", time())?>'
var timeLeft = new Date(serverTime)
var t;
var timeLeft1 = timeLeft
var h = //I'm pulling this value from timer_hour in my database.
var m = //I'm pulling this value from timer_minutes in my database.
var s = //I'm pulling this value from timer_seconds in my database.
//Uses the values from the database to set the timer.
timeLeft1.setHours(h,m,s);
//Add leading zero.
function HHMMSS(digits){
var HHMMSSstring=(digits.toString().length==1)? "0"+digits : digits;
return HHMMSSstring;
}
//Run the timer in a loop and display it.
function displayTimer1(){
var timeString1 = HHMMSS(timeLeft1.getHours())+":"+HHMMSS(timeLeft1.getMinutes())+":"+HHMMSS(timeLeft1.getSeconds());
t=setTimeout("displayTimer1()", 998);
document.getElementById("timer1").innerHTML= timeString1;
if (timeString1=="00:00:00"){stopCount();}
//Reduce the seconds by 1 every time this function loops.
timeLeft1.setSeconds(timeLeft1.getSeconds()-1);
/*I've tried a few methods to UPDATE the "timer_*" fields but here is my latest attempt - sending the new timeLeft1 values to hidden inputs on the page. Like I said, it doesn't work but it should at least illustrate what I'm trying to do. These ID's are defined on the page.*/
document.getElementById("hours1").innerHTML= timeLeft1.getHours();
document.getElementById("minutes1").innerHTML= timeLeft1.getMinutes();
document.getElementById("seconds1").innerHTML= timeLeft1.getSeconds();
//Send new timeLeft1 values back to the server. This is giving me a syntax error.
<?php
$con = mysql_connect("servername","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$hours = "getElementById('hours1')"
$minutes = "getElementById('minutes1')"
$seconds = "getElementById('seconds1')"
mysql_query("UPDATE tablename
SET timer_hour='$hours', timer_minutes='$minutes', timer_seconds='$seconds'
WHERE pod='1'");
mysql_close($con);
?>
}
//Stop the displayTimer1() loop.
function stopCount(){
clearTimeout(t);
}
UPDATE tablename
SET timer_hour='timeLeft1.getHours()', timer_minutes='timeLeft1.getMinutes()', timer_seconds='timeLeft1.getSeconds()'
WHERE pod='1'