John Davidson

document.getElementById with php and javascript

0 comments
Message:


im coding cards with timers, i have my database and this save the hour and date. My problem here is at the moment to print the timers


this code with "demo" on each the fields with $row[0] will display a timer on the screen, but i need one time foreach of the cards, i dont have any idea of how i can do this, my consult i very simple
$row[0]= ID, $row[1]= serial number, $row[2]= street, $row[3]=year, $row[4]=month, $row[5]=day, $row[6]=Hour, $row[7]=minutes, $row[8]=seconds


<?php
$date = $row[3] . "-" . $row[4] . "-" . $row[5];
$time = $row[6] . ":" . $row[7] . "-" . $row[8];
echo $date . " " . $time;
$date_today = $date . ' ' . $time;
?>
<script type="text/javascript">
var count_id = "<?php echo $date_today; ?>";
var countDownDate = new Date(count_id).getTime();

var x = setInterval(function(){
var now = new Date().getTime();
var distance = countDownDate - now;

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(<?php $row[0] ?>).innerHTML = hours + "h " + minutes + "m " + seconds + "s ";

if(distance<0){
clearInterval(x);
document.getElementById(<?php $row[0] ?>).innerHTML="COMPLETO"
}
},1000);
</script>
<?php
echo '<p id="$row[0]" style="font-size:18px;"></p>'
?>

im prety sure my issue is on the document.getELementById


o i forget to tell that im doing all this on PHP/apache2



My Answer:
PHP and JavaScript can help you achieve this. You can use PHP to retrieve the saved hour and date from your database and then use JavaScript to calculate and display the timer on your cards.

Here's an example code snippet to get you started:

php
// Retrieve saved hour and date from database
$hour = "12:00:00"; // Example saved hour
$date = "2022-01-01"; // Example saved date

// Calculate remaining time in milliseconds
$targetDate = strtotime("$date $hour");
$currentDate = time();
$remainingTime = $targetDate - $currentDate;

// Convert remaining time to hours, minutes, and seconds
$hours = floor($remainingTime / 3600);
$minutes = floor(($remainingTime % 3600) / 60);
$seconds = $remainingTime % 60;
?>






In this code snippet, we first retrieve the saved hour and date from the database using PHP. We then calculate the remaining time in milliseconds between the current time and the saved time. We convert this remaining time into hours, minutes, and seconds using PHP.

Next, we use JavaScript to display the timer on the card by updating the timer element every second using `setInterval`.

You can customize this code further to suit your specific requirements and styling preferences.

Rate this post

3 of 5 based on 6214 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us