在线考试时间在PHP的计数器

时间:2018-05-25 06:30:31

标签: php jquery ajax

如何在php中为在线考试创建一个向下计数器,在计数器达到0后它应该自动完成考试我可以使用这些代码如果可能请告诉我如何

    <!-- Display the countdown timer in an element -->
<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();

// 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 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);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

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

2 个答案:

答案 0 :(得分:0)

使用strtotime函数获取日期:

$date = strtotime("Sep 5, 2018 15:37:25");
$remaining = $date - time();

然后$剩余是剩余的秒数。然后你可以将这个数字除以得到天数,小时数,分钟数等。

$daysRemaining = floor($remaining / 86400);
$hoursRemaining = floor(($remaining % 86400) / 3600);

要将它放入JavaScript函数中,它看起来像这样:

<script>
    setInterval(function() {
        <?php
         /*
        It is important that the date field in the database is the proper datatype,
        i.e. DATETIME.
        The 'date' in this select statement is your date field
        The where clause is the condition of a specific selection in your table
        You then replace the hardcoded date with the data stored as a php variable
        */
        $sql = "SELECT date FROM table WHERE something='something'";
        $result = mysql_query($sql);
        $dateData = mysql_fetch_array($result)[0];

        $date = strtotime("".$dateData."");
        $remaining = $date - time();
        $daysRemaining = floor($remaining / 86400);
        $hoursRemaining = floor(($remaining % 86400) / 3600);
        ?>
        var days = <?php echo $daysRemaining; ?>;
        var hours = <?php echo $hoursRemaining; ?>;
        <?php
        if($remaining <= 0) {
            ?>
            document.getElementById("demo").innerHTML = "EXPIRED";
            <?php
        }else {
            ?>
            document.getElementById("demo").innerHTML = days + "d " + hours + "h ";
            <?php
        }
        ?>
    }, 1000);
</script>

编辑:

在看到您需要数据库中的日期后,请参阅已编辑的答案。 如果你使用的话,更改mysql语法以适合mysqli或PDO语法。由于mysql_,我建议您使用mysqli_或PDO(如果还没有) 被弃用。

希望这有帮助。

虽然有问题。如果您知道考试时间,那么仅仅使用那段时间会阻止您的是什么?无论如何,我向您展示了如何从数据库中检索日期。

答案 1 :(得分:0)

正如您在评论中提到的,您需要从数据库中获取日期,以便将其存储在某个字段中并以脚本形式获取

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

我无法告诉您如何从数据库中获取日期,因为您在问题中没有提到有关数据库的任何信息。但我希望你知道如何。所以只需将其存储在$dateFromDB中即可。像这样得到它

var dateDB = document.getElementById("dateFromDB").val();

并使用dateDB代替静态日期

var countDownDate = new Date(dateDB).getTime();