Jump to content

Countdown Timer [php/javascript]


c4ted

Recommended Posts

Every 12 hours a user can vote and I have that part down, but I want to convert the seconds into hours, minutes, and seconds that updates every second so that it count downs from 12:00:00 to 00:00:00. I have $timeleft = 12 hours in seconds - $time where $time is how many seconds have past between now and the last time the user has voted.

<?phprequire('connect.php');$time = time();$query = mysql_query("SELECT * FROM users WHERE username='c4ted");$fetch = mysql_fetch_array($query);$time = $time - $fetch['votetime'];$timeleft = (43200 - $time);echo "$timeleft <br />";if ($time>=43200){echo "good";}else	echo "bad";?>

Link to comment
Share on other sites

Try it with this:This goes to check_vote_time.php with script above:

$last_vote = $fetch['votetime'];$now = time();$last_vote_plus_12_hours = $last_vote+43200;$vote_again = $last_vote_plus_12_hours-$now;

It will result how many secs you must wait till vote.This goes to javascript file, or tags:

var ajax;ajax = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");if (!ajax) { alert("Error while creating page!"); }function get_vote_time() {  ajax.open("GET", "/check_vote_time.php", true);  ajax.onreadystatechange = function() {	if (ajax.readyState == 4) {	  document.getElementById("vote_time").innerHTML = ajax.responseText;	}  }  ajax.send(null);  setTimeout("get_vote_time()", 1000);}get_vote_time();

Also you must make some element, ex: div with id="vote_time"Hope this will work. Also, you can change var names. I wrote it like that to be more readable, and easy to understand.

Link to comment
Share on other sites

You can use basic math and the floor function to figure out how many hours/minutes/seconds are left. For example the number of hours would be:$hours = floor($timeleft/3600); //there are 3600 seconds in an hourThen subtract that from the time left to get the number of seconds left that don't make a full hour:$remaining = $timeleft - ($hours * 3600);And repeat the process for minutes and seconds.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...