为什么我不能将sql数据库中的时间戳与php中的当前时间进行比较?
sql表包含以时间戳形式保存的时间为什么我不能将sql数据库中的时间戳与php中的当前时间进行比较?,php,mysql,Php,Mysql,sql表包含以时间戳形式保存的时间 您可以试试这个 将此代码粘贴到head部分,并用您的网站url替换Anywhere.com <script type="text/javascript"> $(document).ready(function() { if("<?php echo $timezone; ?>".length==0){ var visitortime = new Date(); va
您可以试试这个
将此代码粘贴到head部分,并用您的网站url替换Anywhere.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>
还可以在标题中添加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
}
?>
@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");
}
?>
我会在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.
?>
时间将采用哪种格式,您可以用它更新您的问题吗?它使用时间戳保存在sql中,看起来像2017-04-17 13:00:00@rahul_mif($current_time>$user_time){echo$current_time.\n“$user_time;}//我尝试回显它,但结果是2017-04-17 10:20:53 2017-04-17 15:37:25用户时间正确,但当前时间错误如何更改@Sandeepu您是否设置了日期默认时区?不,我不想要默认时区,它将为每个用户存储在数据库中。我将如何做到这一点?您的$user_time和$current_time应该位于同一时区@janejoeho我将如何做到@如果(time()>$limitTime){echo$current\u time.“\n”。$user\u time;}//我试图回显它,但结果是2017-04-17 10:30:27 2017-04-17 16:58:55$limitTime是正确的,但current time()是错误的@地堡男孩
$timezone = $_SESSION['time'];
<?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
}
?>
<?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");
}
?>
<?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.
?>