为什么我不能将我的sql数据库中的时间戳与php中的当前时间进行比较?

时间:2017-04-17 06:31:45

标签: php mysql

sql表包含时间戳中保存的时间 the format of time looks like this 2017-04-17 13:00:00

string

3 个答案:

答案 0 :(得分:0)

你可以试试这个

将此代码粘贴到您的头部并将someone.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保存到db时,添加一个新字段,如gmttime和insert $ timezone value。

$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
      }
      ?>

答案 1 :(得分: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");
  }
?>

答案 2 :(得分: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.
  ?>