lee2010 Posted March 26, 2011 Share Posted March 26, 2011 hi all, im looking at a countdown timer and when a user places a bid it resets the timer, exactly like the one used at madbid.com, is this a mixture of javascript and php or just php? if you could point me in the right direction of what to look into that would be very helpful Lee Quote Link to comment https://forums.phpfreaks.com/topic/231768-dynamic-countdown/ Share on other sites More sharing options...
aabid Posted March 26, 2011 Share Posted March 26, 2011 As far as my knowledge to web designing knows...it can be done by javascript. This doesn't mean that PHP can't do it though Quote Link to comment https://forums.phpfreaks.com/topic/231768-dynamic-countdown/#findComment-1192479 Share on other sites More sharing options...
monkeytooth Posted March 26, 2011 Share Posted March 26, 2011 This is definitely a JavaScript issue. Although if your storing your timestamps in a database, then yes there is php attached to the concept of a live countdown. But that would be AJAX related, and as far as the PHP goes its as simple as getting the timestamp like you would to display it on the page staticly and not in a live format, the javascript you would use from there would take that same timestamp then apply the live visual to it. Side note easy to help keep logic on what your looking for in the future. PHP renders basicly only when the page is loading server side. JavaScript can manipulate the page and its appearance and well just about anything on a page if done right after the page is loaded, kinda a crude logic but thats my concept of it in a short simple way. Then AJAX style scripting with JavaScript is your bridge between the 2 if you need to do something with PHP but after the page loads. Quote Link to comment https://forums.phpfreaks.com/topic/231768-dynamic-countdown/#findComment-1192484 Share on other sites More sharing options...
lee2010 Posted March 26, 2011 Author Share Posted March 26, 2011 cool thanks, i will look into it Quote Link to comment https://forums.phpfreaks.com/topic/231768-dynamic-countdown/#findComment-1192547 Share on other sites More sharing options...
jcbones Posted March 26, 2011 Share Posted March 26, 2011 This should get you started. <?php session_start(); $diff = 80; //seconds for timer. //MODIFICATION BELOW THIS LINE IS NOT REQUIRED $hld_diff = $diff; $timestamp = time(); //timestamp. /*********************/ //This block is to handle session held countdowns based on the setting of $diff. if(isset($_SESSION['ts'])) { //if timestamp is active in session $slice = ($timestamp - $_SESSION['ts']); //get the difference between the timestamps. $diff = $diff - $slice; //subtract the diff from the ts. } if(!isset($_SESSION['ts']) || $diff > $hld_diff || $diff < 0) { $diff = $hld_diff; $_SESSION['ts'] = $timestamp; } /*******************/ //Below is demonstration of output. Seconds could be passed to Javascript. $diff; //$diff holds seconds less than 3600 (1 hour); $hours = floor($diff / 3600) . ' : '; $diff = $diff % 3600; $minutes = floor($diff / 60) . ' : '; $diff = $diff % 60; $seconds = $diff; ?> <div id="strclock">Clock Here!</div> <div id="clock">Message Here</div> <script type="text/javascript"> var hour = <?php echo floor($hours); ?>; var min = <?php echo floor($minutes); ?>; var sec = <?php echo floor($seconds); ?> function checkTime() { var time = document.getElementById('strclock').innerHTML; if(time == '00:00:00') { window.location.reload(true); } } function countdown() { if(sec <= 0 && min > 0) { sec = 59; min -= 1; } else if(min <= 0 && sec <= 0) { min = 0; sec = 0; } else { sec -= 1; } if(min <= 0 && hour > 0) { min = 59; hour -= 1; } var pat = /^[0-9]{1}$/; secs = (pat.test(sec) == true) ? '0'+sec : sec; mins = (pat.test(min) == true) ? '0'+min : min; hours = (pat.test(hour) == true) ? '0'+hour : hour; document.getElementById('strclock').innerHTML = hours+":"+mins+":"+secs; if(min >= 1) { document.getElementById('clock').innerHTML = min+1+' minutes until timer runs out!'; } else { document.getElementById('clock').innerHTML = sec+' seconds until timer runs out!'; } //checkTime(); setTimeout("countdown()",1000); } countdown(); </script> Quote Link to comment https://forums.phpfreaks.com/topic/231768-dynamic-countdown/#findComment-1192644 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.