I am retrieving a timestamp saved in YYYY:MM:DD HH:MM:SS format from PHP MySQL DB.
And then I want that time to subtract from current time stamp and display in a table to show me that a flight takes off in xyz seconds. Thats what the timer is for.
The table has multiple rows.I have seen various questions but none which answer my problem:
<?php
if (mysqli_num_rows($result)>0)
{
while($row=mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['flightno']; ?></td>
<td><?php echo $row['flightdatetime']; ?></td>
<td><?php echo $row['flightfuel']; ?></td>
<script>var totaltime= <?php echo json_encode($row['flightdatetime']); ?>; </script>
<td ><span id="demo"> </span> </td>
<script>
for(i=0;i<19;i++)
{var ttime=totaltime[i];}
// THE PROBLEM IS HERE SOMEWHERE AND I DONT KNOW HOW TO DEAL WITH THE JSON //ABOVE. Converted php array to json to work with javascript of timer.
var countDownDate = new Date(ttime).getTime();
var x = setInterval(function() {
var now = new Date().getTime();
// Find the distance between now an 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);
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s " ;
// If the count down is over
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000); </script>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
</div>
</body>
</html>