How to make countdown Timer using PHP,Ajax

How to Make Count Down Timer Using PHP And Ajax(java script)

Step 1 Write the below code in test.php.

Write Code for using Java script.and Ajax

<script>
$(document).ready(function(){
  setInterval(function() {
// alert('done');
       
$('#mydiv').load('timer.php');
    }, 3000);
$.ajaxSetup({ cache: false });
});

</script>



html>
  <head>
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
     <script type="text/javascript">
          $(document).ready(function () {
             $('#mydiv').delay(1000).load('timer.php');
          });
      </script>

  </head>
  <body>
     <div id="mydiv"></div>
  <body>
</html>


Step 2 write the below code in timer.php


<?php
 date_default_timezone_set('Asia/Kolkata'); //For some reason I needed this to get the number of hours right, it seems like strtotime and time() get different time zones if you don't specify a time zone
$nextThursday = strtotime("next Friday, 01:00 PM");
$remaining = $nextThursday - time(); //Calculating the number of seconds between no and the specified future date

$days_remaining = floor($remaining / 86400); //Dividing between the number of seconds in a day to get number of days
$hours_remaining = floor(($remaining % 86400) / 3600); //Getting the number of seconds that didn't complete a day and dividing them between the number of seconds in an hour to get the hours remaining
$minutes_remaining = floor((($remaining % 86400) % 3600) / 60);
echo "$minutes_remaining";
$seconds_remaining = (($remaining % 86400) % 3600) % 60;

echo "There are $days_remaining days $hours_remaining hours $minutes_remaining minutes left and $seconds_remaining seconds left";


?>

Save the both file and run.

Output would be 

There are 6 days 20 hours 29 minutes left and 46 seconds left


Comments

  1. Why not just do the whole thing in Javascript?

    ReplyDelete
  2. Thanks for your comment .Yes we can do the same in javascript

    ReplyDelete

Post a Comment

Popular posts from this blog

What is HTML? Complete Beginner’s Guide (with Examples)

HTML Forms — Input, Label, Button & Validation (Explained Like a Teacher With Examples)

Difference Between HTML and HTML5 (2025 Explained) | Features, Advantages & Comparison Table