Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/231768-dynamic-countdown/#findComment-1192484
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/231768-dynamic-countdown/#findComment-1192644
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.