I have a $dbSessionDuration
variable where by using mysqli, it is able to bind the result of data from the query into this variable. The $dbSessionDuration
variable holds time so the variable has a time format as below:
01:30:10
Now that means 1 hour 30 mins and 10 seconds. What I want to do is display $dbSessionDuration
value in a timer so that in above’s example it will start ticking down from 01:30:10 and go all the way to 0 when it stops. My question is how do you create timer and place whatever value $dbSessionDuration
value in the timer to count down to 00:00:00?
Advertisement
Answer
First of all, you have to convert your time in seconds.
JavaScript
x
7
1
<?php
2
3
list($hour,$min,$sec) = explode(':', $dbSessionDuration);
4
$dbSessionDurationTime = mktime(0,0,0,$hour,$min,$sec);
5
6
?>
7
To create a countdown, you have to use Javascript.
JavaScript
1
18
18
1
<script type="text/javascript">
2
var millis = <?php echo $dbSessionDurationTime; ?>
3
4
function displaytimer(){
5
var hours = Math.floor(millis / 36e5),
6
mins = Math.floor((millis % 36e5) / 6e4),
7
secs = Math.floor((millis % 6e4) / 1000);
8
//Here, the DOM that the timer will appear using jQuery
9
$('.count').html(hours+':'+mins+':'+secs);
10
}
11
12
setInterval(function(){
13
millis -= 1000;
14
displaytimer();
15
}, 1000);
16
17
</script>
18
I didn’t test, but that should work!