2017-04-17 60 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. 
    ?>