php datetime

3 Cevap php

i am devloping a quiz site and there is time for x min to answer the quiz. So when user clicks on start quiz link the starttime(current time at this instant )is recored in session .Also the endtime (start_time+ 30 min ) is recored in session and every time he submits a answer the current time is compared with the quiz end time. Only if the current time is less then end_time the answer shold be accepted.

  1. Peki nasıl ben CurrentDateTime alabilirim?
  2. Nasıl x dakika tocurrent bu datetime ekleyebilirsiniz?
  3. I nasıl (<=) datetime comapare olabilir?

Ben tarih zaman kullanmak gerektiğini düşünüyorum. Bu doğru mudur?

3 Cevap

zaman almak için php yerleşik işlevleri kullanabilirsiniz:

 <?php
     $currentTimeStamp = time(); // number of seconds since 1970, returns Integer value
     $dateStringForASpecificSecond = date('Y-m-d H:i:s', $currentTimeStamp);
 ?>

bu kez karşılaştırmak gerekiyor uygulama için, zaman damgası kullanılarak daha uygundur.

<?php
     $start = time();
     $end = $start + (30 * 60); // 30 minutes
     $_SESSION['end_time'] = $end;
?>

sınav sunulduğu sayfa:

<?php
    $now = time();
    if ( $now <= $_SESSION['end_time'] ) {
         // ok!
    }
?>

time () size 1970/01/01 iyi olmalı gibi görünüyor (bir tamsayı), beri saniye şimdiki zaman verecektir.

X dakika eklemek için, sadece buna x * 60 eklemek gerekir istiyorum ve başka iki tamsayılar gibi karşılaştırabilirsiniz.

Source: http://us3.php.net/time