Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Mysql比较日期时间_Php_Mysql_Sql Server - Fatal编程技术网

Php Mysql比较日期时间

Php Mysql比较日期时间,php,mysql,sql-server,Php,Mysql,Sql Server,我在数据库中有varchar字段,该字段的格式类似于“2016年2月10日08:26 PM” 现在我想获取即将到来的数据,那么我如何才能获取这些数据呢 $ctime=strtotime(date("Y-m-d H:i:s")); if($type=='1'){ $books = $objData->getAll("select * from bookings

我在数据库中有varchar字段,该字段的格式类似于“2016年2月10日08:26 PM”

现在我想获取即将到来的数据,那么我如何才能获取这些数据呢

$ctime=strtotime(date("Y-m-d H:i:s"));

if($type=='1'){
    $books = $objData->getAll("select * 
                               from bookings 
                               where custID='".$custID."' 
                                 and fromTime>'".$ctime."'");
} 

但是我没有得到正确的结果,请帮助。

首先,正如评论中提到的,您应该使用正确的日期类型。要回答您的问题,仍然可以实现,使用STR_To_DATE mysql函数

$objData->getAll("select * from bookings where custID=".$custID."'
AND unix_timestamp(STR_TO_DATE(fromTime, '%d %b %Y %h:%i %p')) > ".time());

链接:

当然,您没有得到正确的reslut,因为您正在比较varchar。为什么不将有效类型“datetime”用于datetime?您应该在数据库中使用有效的mysql日期格式,并且仅在浏览器中显示信息时为用户设置格式。处理日期和时间时,请转换为日期时间格式。不要基于字符串数据进行比较。你不把你想做数学运算的数字存储为Varchar,为什么把日期存储为Varchar?同意上面的说法。如果您了解PHP,请编写一个脚本来获取日期信息字符条目,然后插入数据库中的一个新列。祝你好运。在bobby tables访问之前,你应该阅读、理解并开始使用参数化查询。这是可行的,但在1种情况下(如果时间为小时或分钟的00,例如2016年9月19日00:10上午),如果我尝试获取unix_时间戳(STR_to_DATE(fromTime),%d%b%Y%h:%i%p'),它将返回NULL如果我们使用的是am/pm,则应为12h格式
12:10AM
。这是24小时。分钟就可以了。以下是包含所有格式的链接:。如果这是唯一不匹配的情况,那么这可能会起作用:
“从预订中选择*,其中custID=“.$custID.”和unix_时间戳(STR_TO_DATE(替换(fromTime,'00:','12:'),'%d%b%Y%h:%i%p')>。.time()