简体   繁体   English

MySQL查询两个日期之间的时间戳

[英]MySQL query between two dates as timestamps

I have been trying to select items from my database that are between two default dates: now and 60 days ago. 我一直在尝试从数据库中选择两个默认日期之间的项目:现在和60天之前。 However all the queries that I try do not work. 但是,我尝试的所有查询都不起作用。

Here is what I have tried: 这是我尝试过的:

$Now = date("Y-m-d");
$Before = date("Y-m-d", strtotime("-60 days");

// This is try1
$sql = "SELECT * FROM myTable WHERE myTimestamp BETWEEN " . $Before . " AND " . $Now;  

// This is try2
$sql = "SELECT * FROM myTable WHERE myTimestamp >= " . $Before . " AND myTimestamp <= " . $Now; 

I am out of guesses of how to do this. 我不知道该怎么做。 I have looked at the other questions that are the same as this one, but none of the solutions presented work. 我看了其他与此问题相同的问题,但是没有提出的解决方案起作用。

Please note: these queries do not give an errors. 请注意:这些查询不会给出错误。 They just don't retrieve anything. 他们只是不检索任何东西。 I have also used get_defined_vars() to print the dates onto the page. 我还使用了get_defined_vars()将日期打印到页面上。 This is what they show: 这是他们显示的内容:

[Now] => 2016-01-07
[Before] => 2015-11-08

"Please note: these queries do not give an errors." “请注意:这些查询不会给出错误。” - This " . $Before . " AND " . $Now; and you're using 2016-01-07 and 2015-11-08 being strings and not integers . -这个" . $Before . " AND " . $Now;并且您正在使用2016-01-072015-11-08作为字符串而不是整数

Same for " . $Before . " AND myTimestamp <= " . $Now " . $Before . " AND myTimestamp <= " . $Now

Plus, those hyphens are interpreted as MINUS, being a mathematical operation. 另外,这些连字符被解释为MINUS,这是一种数学运算。

Ie: 2016 minus 01 minus 07 etc. resulting in a syntax error. 即:2016 01 07等,导致语法错误。

Therefore, those should read as: 因此,这些内容应改为:

  • WHERE myTimestamp BETWEEN '$Before' AND '$Now' ";

  • WHERE myTimestamp >= '$Before' AND myTimestamp <= '$Now' ";

You're not getting any because you're NOT checking for those syntax errors. 您没有得到任何东西,因为您没有在检查那些语法错误。

Consult http://dev.mysql.com/doc/en/string-literals.html about string literals. 有关字符串文字,请查阅http://dev.mysql.com/doc/en/string-literals.html

Also consult When to use single quotes, double quotes, and backticks in MySQL 另请参阅何时在MySQL中使用单引号,双引号和反引号

You're also not querying nor fetching anything and we have no idea which MySQL API you're using to connect with, or whether you did successfully connect to your database. 您也没有查询或获取任何东西,我们也不知道您要使用哪个MySQL API进行连接,也不知道您是否已成功连接到数据库。

Consult: 请教:

You either use mysqli_fetch_array() or mysqli_fetch_assoc() depending on what you want to do here, and this being a mysqli_ example. 您可以根据需要在这里使用mysqli_fetch_array()mysqli_fetch_assoc() ,这是一个mysqli_示例。

Consult the manuals: 查阅手册:

You should take advantage of using MySQL's built-in date/time functions. 您应该利用MySQL的内置日期/时间功能。

and do not mix them together, it doesn't work that way. 并且不要将它们混合在一起,那样行不通。

Consult: Can I mix MySQL APIs in PHP? 咨询: 我可以在PHP中混合使用MySQL API吗?

Depending on the MySQL API you are using to connect with, here are a few links for error handling. 根据您用于连接的MySQL API,以下是一些错误处理链接。


Footnotes: 脚注:

It seems you are new to MySQL, therefore I suggest you have a look at those links I gave you, finding tutorials and Q&A's here on Stack. 看来您是MySQL的新手,因此,建议您查看我给您的那些链接,在Stack上找到教程和Q&A。

You should look into using a prepared statements also: 您还应该考虑使用准备好的语句:

in order to help against an SQL injection 为了帮助防止SQL注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM