How to store string to Date format?

I retrieved date from database which is stored $str now I want to store Date & time in var countDownDate variable.
Here is my timeCounter.php file

<!DOCTYPE HTML>
<html>
<head> 
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
       text-align: center;
       font-size: 25px;
       margin-top: 0px;
   }
</style>
</head>
<body>
         <p id="demo"></p>

         <input type="hidden" id="dateFromDB" value="<?php echo $dateFromDB;?>">

         <?php
             include_once 'dbConnection.php';
             if (@$_GET['q'] == 'quiz') {
             $eid = @$_GET['eid'];
             $time = @$_GET['time'];
             $retrieve = mysqli_query($con, "SELECT time FROM quiz WHERE eid = '$eid'");
             while($row = mysqli_fetch_array($retrieve)) {
             $minutes = $row['time'];
             //echo $minutes;
             $hours = floor($minutes / 60);
             $min = $minutes - ($hours * 60);
             $sec = 1;
             $str = $hours . ':' . $min . ':' . $sec;
             echo $str;
  }
} 
?>


 <script>
  // Set the date we're counting down to set by teacher
       var onlyDate = new Date().getDate();
       var onlyMonth = new Date().getMonth();
       var onlyYear = new Date().getYear();

       //var countDownDate = new Date(onlyYear,onlyMonth,onyDate,0,0,0).getTime();
       var countDownDate = new Date("May 25, 2021 22:39:59").getTime(); //set by teacher

         // Update the count down every 1 second
         var x = setInterval(function() {

          // Get today's 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 + ":" + minutes + ":" + seconds;

                   // If the count down is over, write some text 
               if (distance < 0) {
                clearInterval(x);
               document.getElementById("demo").innerHTML = "Time Over";
           }
           }, 1000);
             </script>

             </body>
             </html>

You can use PHP inside your script tag to echo the date in the correct place:

var countDownDate = new Date("May 25, 2021 <?php echo $str ?>").getTime();

A few other pointers:

  • Never use @ in PHP, it hides problems. In this scenario you’re doing it to squash the errors you get if a request variable isn’t set; instead, just check the variables are set before referencing them. This also allows you to do something more sensible if the variables aren’t set.

  • Investigate and use prepared statements to prevent attacks on your database - sticking request parameters straight into your SQL is dangerous. PHP Delusions has a good article on this.

  • Your code will be easier to follow if you keep your PHP separate from your HTML. You can do this by storing it in a separate file and including it in this script with an include call, or by just moving it to the top of the file.

  • Use descriptive variable names. $str doesn’t help you see what this code does when you come back to it in a few weeks.

Yes got it. Thank You

Sponsor our Newsletter | Privacy Policy | Terms of Service