Hello,
This is some test code that is not producing the results that I expected. The program starts a session and then checks to see if a session variable has been created, if not , it uses the time() function to get a timestamp and assigns that to the session variable and then assigns this timestamp variable a variable called $currentTimeStamp and then assigns this value to another variable called $startTimeStamp. Then it assigns the value of 0 to a variable called $elapse since no time has elapsed on the start of the program. Next the program uses the $elapsed variable in a javascript routine that I found online but modified it slightly. Then it outputs an html page via heredoc. On the initial start the program works as I would expect, but on refresh, I would expect the elapsed time to be deducted from the start time but this is not occurring. However, when I uncomment this line: //$elapsed = rand(10, 1000000); the program works exactly as I would expect it to (I did this just for a test) the timer starts at random times or the timer never starts as you might expect because of the random values, but when I uncomment this line, the timer always starts from the beginning as if the program is ignoring the $elapsed variable even though you can plainly see from the test output that the $elapsed variable value is increasing on each refresh of page. This does not make sense to me. Here is the code with the test variables output to the html:
<?php
session_start();
if (isset($_SESSION['startsession'])){
$startTimeStamp = $_SESSION['startsession'];
$currentTimeStamp = time();
$elapsed = $currentTimeStamp - $startTimeStamp;
}
else{
$_SESSION['startsession'] = time();
$currentTimeStamp = time();
$startTimeStamp = $currentTimeStamp;
$elapsed = 0;
}
//$elapsed = rand(10, 1000000);
$myStuff = <<< A1a
<script>
// Set the date we're counting down to
var countDownDate =new Date().getTime() + (3.6e+6 - $elapsed);
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
//var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
A1a;
echo <<< myVar
<html>
<head><title>My Head</title></head>
<body>
$myStuff
<p id="demo"></p>
<p>Body page 1</p>
<a href = "timerTestPhp.php">Go Back</a>
<p>
StartTimeStamp: $startTimeStamp
</p>
<p>
CurrentTimeStamp: $currentTimeStamp
</p>
<p>
Elapsed: $elapsed
</p>
</body>
</html>
myVar;
?>