我们可以在Mysql Query中使用PHP函数strtotime [英] Can we use PHP function strtotime in Mysql Query
本文介绍了我们可以在Mysql Query中使用PHP函数strtotime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下MySQL语法,这给了我一个错误.
I have the following MySQL syntax which gives me an error.
我知道您不能直接比较日期变量,因此我使用strtotime
创建Unix时间戳来比较日期.
I am aware that you cannot compare date variables directly, so I use strtotime
to create a Unix timestamp to compare dates.
您可以在MySQL查询中使用PHP strtotime
函数吗?
Can you use the PHP strtotime
function within a MySQL query?
$result = select * from table where strtotime(Date) >= strtotime($_POST[DateFrom1])
&& strtotime(Date) <= strtotime($_POST[DateTo1])";
推荐答案
您的代码是NOT
有效的PHP语法.下面的代码是一种干净而正确的方法:
Your code is NOT
a valid PHP syntax. Code below is a clean and proper way of doing this:
$from = date('Y-m-d', strtotime($_POST["DateFrom1"]));
$to = date('Y-m-d', strtotime($_POST["DateTo1"]));
$query = "select * from table where date between '$from' and '$to'";
// use this query to your mysqli
这篇关于我们可以在Mysql Query中使用PHP函数strtotime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文