apw Posted February 6, 2013 Share Posted February 6, 2013 Hello I wrote a simple but effective countdown script .. you simply plug in the time (*nix time) that you want the script to end and it will countdown from now using the time(); function .. which is all good in a way, however here is what I'm running into the problem. I included the code below that I use for this simple script but as the time goes on, poor player has to keep hitting refresh for the time to update. This is because I store the time(); in my database along with the future time. Instead of poor player A having to keep hitting the refresh button is there a way I can script this to where no refresh is needed and the time continues to update then when the $ctime (current time) hits $etime (future time) the script stops and say it updates the database? I figured $etime by taking this and doing a simple: $etime = 30; Time I want to advance the current real time. So .. then I subtracted $etime - $ctime to get the difference But you already knew that .. If you could give me a hand, I'd greatly appreciate it, even simpilify my messy code to possibly get the result I'm looking for .. How about a javascript .. would that work as well? I have an exaple below the first set of code the code runs until the timer reaches 0 then restarts .. instead of restarting maybe have the code just end and preform a DB update? // Sets and posts current time to DB $my_time=time(); // current time $update_time="update bbg_timers set ctime='$my_time' where username='$username'"; mysql_query($update_time) or die("could not query"); $time_remain2=$etime-$ctime; // $time_remain2=$ctime-$etime; $difference=$time_remain2; $difference=$time_remain2; $hours_difference = floor($difference / 3600); $difference = $difference % 3600; $minutes_difference = floor($difference / 60); $seconds_difference = $difference % 60; $secshow = false; $timeleft = ''; if ($hours_difference > 0) { $timeleft .= $hours_difference . 'h '; } else { $secshow = true; } if ($diff > 60) { $timeleft .= $minutes_difference . 'm '; } elseif ($diff > 60 && !$seconds) { $timeleft = '<1m'; } if ($secshow) { $timeleft .= $seconds_difference . 's '; } if ($diff < 0) { print "Diff is < 0"; } if (($diff * 60) < 15) { // print "You have $hours_difference hours, $minutes_difference minutes, and $seconds_difference remaining"; } } Here is the other javascript code I was talking about -- I took this from the footer file of my bnt game (giving credit) so I'm not exactly sure what some of the code means // Update counter $res = $db->Execute("SELECT last_run FROM $dbtables[scheduler] LIMIT 1"); $result = $res->fields; $mySEC = ($sched_ticks * 60) - (TIME()-$result[last_run]); ?> <script language="javascript" type="text/javascript"> var myi = <?=$mySEC?>; setTimeout("rmyx();",1000); function rmyx() { myi = myi - 1; if (myi <= 0) { myi = <? echo ($sched_ticks * 60); echo "\n";?> } document.getElementById("myx").innerHTML = myi; setTimeout("rmyx();",1000); } </script> <? echo " <b><span id=myx>$mySEC</span></b> $l_footer_until_update <br>\n"; // End update counter Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/274087-counting-the-time-down-question-please-help/ Share on other sites More sharing options...
apw Posted February 6, 2013 Author Share Posted February 6, 2013 Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/274087-counting-the-time-down-question-please-help/#findComment-1410573 Share on other sites More sharing options...
Jessica Posted February 6, 2013 Share Posted February 6, 2013 AJAX! Quote Link to comment https://forums.phpfreaks.com/topic/274087-counting-the-time-down-question-please-help/#findComment-1410584 Share on other sites More sharing options...
apw Posted February 6, 2013 Author Share Posted February 6, 2013 Ajax? Quote Link to comment https://forums.phpfreaks.com/topic/274087-counting-the-time-down-question-please-help/#findComment-1410585 Share on other sites More sharing options...
Jessica Posted February 6, 2013 Share Posted February 6, 2013 AJAX! Quote Link to comment https://forums.phpfreaks.com/topic/274087-counting-the-time-down-question-please-help/#findComment-1410587 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.