PHP - Countdown Timer On Page
Newbie PHP user here, so go easy on me .
I've been trying to make a timer that starts on an arbitrary number of seconds, say 10 for example, then counts down to 0 (and refreshes itself every second for the user to see how much time is remaining), and then some more code is executed when 0 is reached. I attempted to do something like: <?php $goal = time() + 10; while($goal - time() >= 0) { echo "$goal - time()"; sleep(1); } ?> This didn't seem to work as my page just took a while to load - I suspect it was going through the entire while loop before displaying anything rather than just dynamically updating the timer every second. Also when I chose 100 instead of 10, the webpage didn't seem to even finish loading. Also it displayed a lot of odd text - not a timer at all! Help would be much appreciated ! Similar TutorialsCan anyone help me to make a countdown timer in php? so basically i've made a quiz.. but i want it to have a time of 1 hour 30 mins.. but i dont have any idea of how to make a timer Here's the code for the countdown timer. It works. At the end of the countdown, it's suppose to show the message "EXPIRED". But that message only shows once I reload the page. The countdown itself stops at 00:00:01. Is there a way to automatically show the message after that, instead of reloading the page to show it? <style> div#counter{ margin: 100px auto; width: 305px; padding:20px; border:1px solid #000000; } div#counter span{ background-color: #00CAF6; padding:5px; margin:1px; font-size:30px; } </style> <?php $target_date = '2019-01-12 05:40:00'; $timeLeft = (strtotime($target_date) - time()) * 1000; ?> <script src="javascripts/timer.js"></script> <script> $(document).ready(function(){ var timeLeft = <?php echo $timeLeft ; ?>; var timer = new Timer($('#counter'), timeLeft); if (timeLeft <= 0) { $('#counter').text('EXPIRED'); } }); </script> <div id="counter"> <span class="hour">00</span> <span class="min">00</span> <span class="sec">00</span> </div> Edited January 11, 2019 by imgrooot Hi all, I would like to create a timer showing a countdown from 0% to 100% (representing "power") ending at noon GMT on Sunday 4th September. The countdown started a few weeks ago, hence as of 1pm GMT on Tuesday 1st March, the countdown should be at 15% The "power" increases by 5% every 11 days so by my calculations, the percentage in our countdown should increase by 0.01% every 1900.8 seconds. So, with this theory, how would I go about coding it? I guess since I use GMT, I need to take a timestamp in GMT first, then calculate the difference between this and Sunday 4th September at 12.00pm. I am not sure though which format of time/date is best to do the calculation. Would it be better to use Unix time? Any help, suggestions and/or code is greatly appreciated. Skulty Hey guys. My question is simple. I have a time value of the future, in seconds, that I extract from MySQL. time(); How do I make an interactive countdown timer using it? It should show the numbers ticking away. I tried Javascript, but it doesn't go with the php time layout. Can anyone help me here? Thanks! Hi Guys! Hoping someone can help me with this. I need to produce a timer which I can embed on a website. It needs to count down the remaining hours until 7pm during the weekdays, then at the weekends count down until 7pm on Monday. Any ideas or points in the right direction would be greatly appreciated! I'm a bit of a newbie! Hey guys and gals!
I am currently working on implementing the following functionality in one of my pages:
Whenever a person with a specific IP address visits the page, an internal countdown timer of 2 hours should be started. Until that timer is active, the only response from the page ANYONE can get would be a predefined echo value. Once the timer has run out, the normal script execution of the rest of the page should be restored.
Any pointers and tips on how to approach that would be greatly appreciated.
So I have a count down timer on my site. http://fpsboost.net And I am clueless as to how I would set a final date with this. Cant find where it's grabbing the info for "finaldate" or anything like that. Want to set the final date to feb 1st 2015 Javascript newb here. Anyone willing to help would be greatly appreciated. Thanks in advance! Btw, I'm using a generic js countdown timer from: http://hilios.github...uery.countdown/ called "The Final Countdown for jQuery v2.0.4" It was preloaded in a template that I downloaded for my site. Edited by jakobe, 09 December 2014 - 01:09 AM. Hi again people! I found this code that codes for a countdown timer. My problem is, how do I implement it? I've found similar codes that leave me dazzled as I try to get them to work. The URL from which I found this is: http://scripts.franciscocharrua.com/server-side-countdown-clock.php The code is: Code: [Select] function countdown_clock(year, month, day, hour, minute, format) { //I chose a div as the container for the timer, but //it can be an input tag inside a form, or anything //who's displayed content can be changed through //client-side scripting. html_code = '<div id="countdown"></div>'; document.write(html_code); Today = new Date(); Todays_Year = Today.getFullYear() - 2000; Todays_Month = Today.getMonth(); <? $date = getDate(); $second = $date["seconds"]; $minute = $date["minutes"]; $hour = $date["hours"]; $day = $date["mday"]; $month = $date["mon"]; $month_name = $date["month"]; $year = $date["year"]; ?> //Computes the time difference between the client computer and the server. Server_Date = (new Date(<?= $year - 2000 ?>, <?= $month ?>, <?= $day ?>, <?= $hour ?>, <?= $minute ?>, <?= $second ?>)).getTime(); Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(), Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime(); countdown(year, month, day, hour, minute, (Todays_Date - Server_Date), format); } function countdown(year, month, day, hour, minute, time_difference, format) { Today = new Date(); Todays_Year = Today.getFullYear() - 2000; Todays_Month = Today.getMonth(); //Convert today's date and the target date into miliseconds. Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(), Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime(); Target_Date = (new Date(year, month, day, hour, minute, 00)).getTime(); //Find their difference, and convert that into seconds. //Taking into account the time differential between the client computer and the server. Time_Left = Math.round((Target_Date - Todays_Date + time_difference) / 1000); if(Time_Left < 0) Time_Left = 0; switch(format) { case 0: //The simplest way to display the time left. document.all.countdown.innerHTML = Time_Left + ' seconds'; break; case 1: //More datailed. days = Math.floor(Time_Left / (60 * 60 * 24)); Time_Left %= (60 * 60 * 24); hours = Math.floor(Time_Left / (60 * 60)); Time_Left %= (60 * 60); minutes = Math.floor(Time_Left / 60); Time_Left %= 60; seconds = Time_Left; dps = 's'; hps = 's'; mps = 's'; sps = 's'; //ps is short for plural suffix. if(days == 1) dps =''; if(hours == 1) hps =''; if(minutes == 1) mps =''; if(seconds == 1) sps =''; document.all.countdown.innerHTML = days + ' day' + dps + ' '; document.all.countdown.innerHTML += hours + ' hour' + hps + ' '; document.all.countdown.innerHTML += minutes + ' minute' + mps + ' and '; document.all.countdown.innerHTML += seconds + ' second' + sps; break; default: document.all.countdown.innerHTML = Time_Left + ' seconds'; } //Recursive call, keeps the clock ticking. setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + time_difference + ', ' + format + ');', 1000); } I've tried saving the file as a php file and html file, but to no avail... Hi, This is my first post so dont kill me if i did somthing wrong. Im trying to make a simple php/javascript page that will display the time remaining in each period every day (we have 4 periods per day). I found a nice javascript library from www.hashemian.com and am using the example that he linked to to do multiple countdown timers on one page. The problem i am having is that the only way i can think of to make it so that it counts down to a dynamic date is to specify that date as a variable and then combine it with the string that specifys the rest of the date/time combo for the target date/time. The current live version is at smd75jr.com/test/index2.php The first timer is just a test that im using to make sure i havent completely broken it. THe second timer is the one im trying to troubleshoot, it is currently set for 11:59 PM EST "today". (see code below) Any help would be greatly appreciated!! This is my code: Code: [Select] <html> <head> <title>Multiple Countdown Clocks</title> </head> <body> <div id="clock1">[clock1]</div> <div id="clock2">[clock2]</div> </body> <script language="JavaScript"> StartCountDown("clock1","06/27/2012 9:33 PM -0400") StartCountDown("clock2","periodTest") TodaysDate() //var today = new Date() //var todayMonth = today.getMonth() + 1 //var todayDay = today.getDate() //var todayYear = today.getFullYear() //var todayDate = (todayMonth + "/" + todayDay + "/" + todayYear) var periodTest = (todayDate + " 11:59 PM -0400") var periodA1 = (todayDate + " 8:53 AM -0400") var periodA2 = (todayDate + " 10:26 AM -0400") var periodA31 = (todayDate + " 12:32 PM -0400") var periodLunch1 = (todayDate + " 11:00 AM -0400") var periodLunch2 = (todayDate + " 12:32 PM -0400") var periodA32 = (todayDate + " 11:59 AM -0400") var periodA4 = (todayDate + " 2:05 PM -0400") //function Periods(todayDate, periodA1, periodA2, periodA31, periodA32, periodA4, periodB1, periodB2, periodB31, periodB32, periodB4, periodLunch1, periodLunch2, periodSchoolStart, periodSchoolEnd) // { // var today = new Date() // var todayMonth = today.getMonth() + 1 // var todayDay = today.getDate() // var todayYear = today.getFullYear() // var todayDate = (todayMonth + "/" + todayDay + "/" + todayYear) // // var periodA1 = (todayDate + " 21:25 PM -0400") // } function TodaysDate(todayDate) { var today = new Date() var todayMonth = today.getMonth() + 1 var todayDay = today.getDate() var todayYear = today.getFullYear() var todayDate = (todayMonth + "/" + todayDay + "/" + todayYear) } /* Author: Robert Hashemian (http://www.hashemian.com/) Modified by: Munsifali Rashid (http://www.munit.co.uk/) Modified by: Tilesh Khatri */ function StartCountDown(myDiv,myTargetDate) { var dthen = new Date(myTargetDate); var dnow = new Date(); ddiff = new Date(dthen-dnow); gsecs = Math.floor(ddiff.valueOf()/1000); CountBack(myDiv,gsecs); } function Calcage(secs, num1, num2) { s = ((Math.floor(secs/num1))%num2).toString(); if (s.length < 2) { s = "0" + s; } return (s); } function CountBack(myDiv, secs) { var DisplayStr; var DisplayFormat = "%%D%% Days %%H%%:%%M%%:%%S%%"; 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)); if(secs > 0) { document.getElementById(myDiv).innerHTML = DisplayStr; setTimeout("CountBack('" + myDiv + "'," + (secs-1) + ");", 990); } else { document.getElementById(myDiv).innerHTML = "Period Over"; } } </script> </html> Hey all! In the code in question I echo out individual records of data from MySQL successfully. For each record there is a number which is used as a var in the javascript that does the count-down-timer part. However when I view the resulting page the timer works dynamically only with the first record. With the rest, the timer is static. Code: [Select] <? $result0 = mysql_query("SELECT * FROM table WHERE field='$value'"); while ($riw0 = mysql_fetch_assoc($result0)) { $seconds1 = $riw0['seconds'] ; //// echo out data and set variable for the number of seconds to count down ?> <script language="JavaScript"> var countDownInterval=<?=$seconds1?>; var c_reloadwidth=200 </script> <ilayer id="c_reload" width=&{c_reloadwidth}; ><layer id="c_reload2" width=&{c_reloadwidth}; left=0 top=0></layer></ilayer> <script> var countDownTime=countDownInterval+1; function countDown(){ countDownTime--; if (countDownTime <=0){ countDownTime=countDownInterval; clearTimeout(counter) window.location.href="military3.php" //Redirection URL return } var mins = Math.floor(countDownTime/60) var secs = countDownTime-(mins*60) if (document.all) //if IE 4+ document.all.countDownText.innerText = mins+" minutes "+secs+ " "; else if (document.getElementById) //else if NS6+ document.getElementById("countDownText").innerHTML=mins+" minutes "+secs+ " " else if (document.layers){ document.c_reload.document.c_reload2.document.write('Soldiers will be ready in... <span id="countDownText">'+countDownTime+' </span> seconds') document.c_reload.document.c_reload2.document.close() } counter=setTimeout("countDown()", 1000); } function startit(){ if (document.all||document.getElementById) document.write('Soldiers will be ready in <span id="countDownText">'+countDownTime+' </span> seconds') countDown() } if (document.all||document.getElementById) startit() else window.onload=startit </script> <? } ?> I tried replacing the javascript vars with PHP echoes for unique variables, but then no timer shows up, even static. So could anyone advice me on how I could use this code to apply for all MySQL records? Thanks in advance, Thauwa P.S. If I am unclear with my quandary, do let me know. Thank you. Hi, i dont know how to explain this so ill start with what i want to do. Id like to create a simple text based game in php for learning purposes but im stuck on 1 thing, making a page refresh based on a server timer. Basically every 2 minutes is a server tick and at such time your character gets +gold based on resources etc. im just not sure what such a thing would be called so i havnt been able to google it, obviously the ticks need to be server controlled so a user cant just refresh a page to get a new tick. i would also need the page to display how long left till next tick. the only way i can see it possible is to have a server side application controlling timers, and the php page requests time remaining or something. Any help would be muchly appreciated, thanks. <?php for($test=100; $test!=0; $test--){ echo $test."\n"; sleep(1);} ?> Instead of counting down like 100 99 98 etc how can I make it replace the previous number? So it would just be like 100 then 100 would go away and 99 would replace it and so on Any help is appreciated Can someone show me the way? I am creating a online examination with time pressured. For example Part I of examination is only good for 20 mins. now if 20 mins is over it will be lock or good as submit. How will I do that? tnx everyone. Hey Guy's
I need a quick fix to solve an urgent problem..
I used to do some coding years ago but the old grey matter is not so responsive these days and I have been up all night trying to resolve a problem.
I have built a website for my daughters and created a simple web form but I have come unstuck. All those skills I once took for granted are gone.
So here's where I am at. I've created an input form_mail.html, a contact.php and a reply.html page uploaded them to the Server and hit an error when I tried to send a message. The problem it seems is with the PHP.ini File.
In short I don't have one Duh! Yes I know I should go back and study and that much I intend to do I am currently installing xxampp, MySQL and php on my new laptop.
But in the meantime I need your help urgently..
What code do I need in the php.ini file to make the Form Mail work?
Is there somewhere on the web where I can see a simple code, I am just too tired to wade through heavy docs right now.
Your help would be much appreciated at this time.
Sincerely
Tam
How am i able to setup a countdown timer? I use the time(); function and want to compare that time with another time that is 8 hours ahead to form a countdown timer, how am i able to do this using php? Hello. Well i'm trying to make a timer to execute some code. Basically I want 3 minutes, every 3 minutes a piece of code is activated, then another 3 minutes later it does so again. But I have tried different ways of doing this, I tried with cookies and found out the person with the cookie is the only one that can refresh the page to activate the code. Same with sessions. And all the others are usually interrupted and start the timer again when the page reset. So yeah, I don't want it to be interrupted by refreshing. So any help please? I also tried with timestamps, this has proven to be the best way but I can never get it working 100% Hi, how would I go about making a countdown to midnight that will auto reset each time it hits midnight. I would like it to use server time and it doesn't have to be live, can just change when the user refreshes. I would like to have this done in php but am about to lose my mind. Just need a script to countdown to midnight, server time. Doesn't have to be live, just something to see when the page is reloaded. Any help would be much aprreciated Im looking for some example code for countdowns. I need to be able to count down to the same time each day for example 9PM each day. and be displayed as 5hr 8m 15s. When the timer runs out, then run some code then start again for the next day. what would be the best route to take for this? Thanks in advance i am using countdown timer,it gets remaining time in seconds and displays cont down.but when i changed the remaining time for the countdown it displays the same old timer and did not update its countdown time. i need a deperate help for it , can some help me. |