2017-04-17 49 views
0

SQL表包含保存在時間戳的時間 the format of time looks like this 2017-04-17 13:00:00爲什麼我不能比較我的sql數據庫中的時間戳與php中的當前時間?

<?php 
    include 'con.php'; 
    $token = $_GET['key']; 
    $sql = "Select * FROM userlogin WHERE token = '$token'"; 
    $result = mysqli_query($connect,$sql); 
    $user = mysqli_fetch_array($result); 

    if($user['time'] > 1800) {   // check if the time is greater than 30 minutes then the link will expire. 
    exit("Link has expired"); 
    } 
    ?> 
+1

時間將以哪種格式顯示,你可以用那個更新你的問題嗎? – rahulsm

+0

它使用時間戳保存在sql中,它看起來像這樣2017-04-17 13:00:00 @rahul_m – janejoe

回答

0

你可以試試這個

在你的頭上這部分代碼粘貼,並與你的網站URL代替anyone.com

<script type="text/javascript"> 
    $(document).ready(function() { 
     if("<?php echo $timezone; ?>".length==0){ 
      var visitortime = new Date(); 
      var visitortimezone = "GMT " + -visitortime.getTimezoneOffset()/60; 
      $.ajax({ 
       type: "GET", 
       url: "http://anyone.com/timezone.php", 
       data: 'time='+ visitortimezone, 
       success: function(){ 
        location.reload(); 
       } 
      }); 
     } 
    }); 
</script> 

並創建一個新文件timezone.php並在那裏寫入

<?php 
    session_start(); 
    $_SESSION['time'] = $_GET['time']; 
?> 

並啓動會議在頭

<?php session_start(); ?> 

當你保存的UserDetails到數據庫中添加一個新的領域像GMTTIME並插入 $區值。

$timezone = $_SESSION['time']; 

而且在頭

添加jQuery的新代碼將是

<?php 
     include 'con.php'; 
     $token = $_GET['key']; 
     $sql = "Select * FROM userlogin WHERE token = '$token'"; 
     $result = mysqli_query($connect,$sql); 
     $user = mysqli_fetch_array($result); 

     $time = $user['time']; 
     $gmt = $user['gmttime']; 

     $timezone2 = str_replace("GMT", "", $gmt); 

     $sec = 3600 * $timezone2; 

     $gmtdate = gmdate("Y-m-d H:i:s"); 

     $current_time = date("Y-m-d H:i:s", strtotime($gmtdate) + ($sec)); 

     $user_time = date("Y-m-d H:i:s", strtotime($time) + 1800); 




     if($current_time > $user_time) {   // check if the current time is greater than $user['time']+30 minutes 
     exit("Link has expired"); 
     } 
     else{ 
     //your code part here 
     } 
     ?> 
+0

if($ current_time> $ user_time){echo $ current_time。「\ n」。$ user_time; }//我試圖迴應它,但結果是2017-04-17 10:20:53 2017-04-17 15:37:25 usertime是正確的,但目前的時間是錯誤的我怎麼能改變它? @Sandeep – janejoe

+0

您是否設置了日期默認時區? – Sandeep

+0

不,我不想要一個默認的時區,它會存儲在每個用戶的數據庫中,我將如何做到這一點? – janejoe

0

@janejoe你缺少的東西只是糾正你的條件,如:

<?php 
    include 'con.php'; 
    $token = $_GET['key']; 
    $sql = "Select * FROM userlogin WHERE token = '$token'"; 
    $result = mysqli_query($connect,$sql); 
    $user = mysqli_fetch_array($result); 
    $limitTime = date("Y-m-d H:i:s", strtotime($user['time']) + 1800); 
    if(date("Y-m-d H:i:s") > $limitTime) {   // check if the time is greater than 30 minutes then the link will expire. 
    echo "current date time :". date("Y-m-d H:i:s"); 
    exit("Link has expired"); 
    } 
?> 
+0

如果(time()> $ limitTime){echo $ current_time。「\ n」。$ user_time; } //我試圖迴應它,但結果是2017-04-17 10:30:27 2017-04-17 16:58:55 $ limitTime是正確的,但當前時間()是錯誤的。 @Bunker男孩 – janejoe

+0

@janejoe請嘗試更新一個 –

0

我會在MySQL中做到這一點。

<?php 
    include 'con.php'; 

    $token = $_GET['key']; 

    $sql = "SELECT * FROM userlogin WHERE token = '$token' AND time BETWEEN TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 MINUTE)) AND TIMESTAMP(NOW())"; 
    $result = mysqli_query($connect, $sql); 
    if (!mysqli_num_rows($result)) { 
    exit("Link has expired"); 
    } 

    $user = mysqli_fetch_array($result); 

    // etc. 
    ?>