php 日期时间和 SELECT * FROM table WHERE date = TODAY
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19361094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Datetime and SELECT * FROM table WHERE date = TODAY
提问by Treps
The column (date) in the database says "2013-10-14 14:37:38" for example.
例如,数据库中的列(日期)表示“2013-10-14 14:37:38”。
How can I edit $todaysDate
to display ALL dates today? I know that I have to use % somewhere but I have tried a lot of ways without success. :/
如何编辑$todaysDate
以显示今天的所有日期?我知道我必须在某处使用 % 但我尝试了很多方法都没有成功。:/
My code is below:
我的代码如下:
$todaysDate = date("Y-m-d");
$query_today = $db->prepare("SELECT * FROM table WHERE date=:date");
$query_today->bindParam(":date", $todaysDate);
$query_today->execute();
UPDATENothing below is working. I just want to select all dates in the database from a simple date("Y-m-d")
. If the date is "2013-10-14 14:37:38" in the database I want to select it with just 2013-10-14. Something like date("Y-m-d%")
.
更新下面没有任何工作。我只想从一个简单的date("Y-m-d")
. 如果数据库中的日期是“2013-10-14 14:37:38”,我只想用 2013-10-14 选择它。类似的东西date("Y-m-d%")
。
回答by Glavi?
Example, if you will search other dates beside today:
例如,如果您要搜索今天以外的其他日期:
$todaysDate = date("Y-m-d"); # or any other date
$query_today = $db->prepare("SELECT * FROM table WHERE DATE(date) = :date");
$query_today->bindParam(":date", $todaysDate);
$query_today->execute();
Example, only for current date:
示例,仅适用于当前日期:
$query_today = $db->prepare("SELECT * FROM table WHERE DATE(date) = CURDATE()");
$query_today->execute();
p.s. but like @ypercubesaid, if you have index on date field, his example will use that index.
ps 但就像@ypercube说的,如果您在日期字段上有索引,他的示例将使用该索引。
$todaysDate = date("Y-m-d");
$query_today = $db->prepare("SELECT * FROM table WHERE date >= :date AND date < DATE_ADD(:date, INTERVAL 1 DAY)");
$query_today->bindParam(":date", $todaysDate);
$query_today->execute();
or
或者
$query_today = $db->prepare("SELECT * FROM table WHERE date >= CURDATE() AND date < DATE_ADD(CURDATE(), INTERVAL 1 DAY)");
$query_today->execute();
回答by ypercube??
This will work whether the column is of type DATE
, DATETIME
or TIMESTAMP
and will use an index, if there is one available:
无论该列是 type DATE
,DATETIME
还是TIMESTAMP
将使用索引(如果有可用索引),这都将起作用:
WHERE date >= CURRENT_DATE
AND date < CURRENT_DATE + INTERVAL 1 DAY
回答by Ben Fortune
A simple
一个简单的
SELECT * FROM table WHERE date = DATE(NOW())
should suffice.
应该足够了。
Or as Alma pointed out,
或者正如阿尔玛指出的那样,
SELECT * FROM table WHERE date = CURDATE()
回答by Code L?ver
Instead of below code:
而不是下面的代码:
$query_today = $db->prepare("SELECT * FROM table WHERE date=:date");
Use the following code:
使用以下代码:
$query_today = $db->prepare("SELECT * FROM table WHERE DATE_FORMAT(date, '%Y-%m-%d')=:date");
回答by andy
$query_today = $db->prepare("SELECT * FROM table WHERE DATE(date)=DATE(:date)");
$query_today = $db->prepare("SELECT * FROM table WHERE DATE(date)=DATE(:date)");