[英]How to compare two datetime in mysql query using php
我想比较两个datetime
变量女巫,其中一个是当前server time
,一个存储在mysql
数据库中,我正在运行这段简单的代码,它无法正常工作,并说这是语法错误,我想知道我该怎么办
<?php include("includes/student_session.php");?>
<?php confirm_logged_in();?>
<?php include("includes/connection.php");?>
<?php
date_default_timezone_set("");
$now_date=date("Y-m-d H:i:s");
$query1="select exam_id from exam where {$now_date} > start_date";
$result=mysqli_query($cnn,$query1);
if($result){
$row=mysqli_fetch_array($result);
echo $exam_id. "exists";
}else
{echo mysqli_error($cnn);}
?>
在此代码中:
$query1="select exam_id from exam where {$now_date} > start_date";
$result=mysqli_query($cnn,@query1); // <-
您正在使用@query1
,它应该是$query1
。
$query1="select exam_id from exam where {$now_date} > start_date";
$result=mysqli_query($cnn,@query1);
我认为错误是因为您使用的是$ insteal。 您的查询也似乎是错误的。 您应该将值与将其更改为的字段进行比较
$query1="select exam_id from exam where start_date > '{$now_date}'";
$result=mysqli_query($cnn,$query1);
即使更正了@query1
的错误类型,查询也不会像这样工作。 $now_date
是一个字符串,您必须像字符串一样将其传递给查询。 如果您使用mysql的函数(而不是php的函数)格式化它,以及将数据库中存储的时间与数据库自身的时间进行比较(甚至可以跳过任何时区问题),则更好。 因此,例如,您的查询可能像这样:
SELECT exam_id FROM exam WHERE DATEDIFF(NOW(), DATE_FORMAT(start_date,'%Y-%m-%d %T')) > 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.