php Mysql查询:检索当前日期查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1086297/
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
Mysql query: retrieve current date query
提问by jingleboy99
In mysql database i have this column called:
在 mysql 数据库中,我将此列称为:
Name: Date
名称日期
Type: datetime
类型:日期时间
I have few values in that column:
我在该列中有几个值:
2009-01-05 01:23:35
2009-01-05 01:23:35
2009-03-08 11:58:11
2009-03-08 11:58:11
2009-07-06 10:09:03
2009-07-06 10:09:03
How do I retrieve current date? I am using php.
如何检索当前日期?我正在使用 php。
in php:
在 php 中:
<?php $today = date('Y-m-d');?>
How to write a mysql query to retrieve all today date data?
如何编写一个 mysql 查询来检索所有今天的日期数据?
Should i change the column type to "date", then insert values like "2009-07-06" only, no time values???
我应该将列类型更改为“日期”,然后仅插入“2009-07-06”之类的值,而不插入时间值吗???
回答by Paolo Bergantino
You don't need to use PHP, MySQL has a function to get the current date:
你不需要使用 PHP,MySQL 有一个获取当前日期的函数:
SELECT field FROM table WHERE DATE(column) = CURDATE()
If your column is only ever going to need the date part and never the time, you should change your column type to DATE. If you insist on doing it through PHP, it is the same thing, really:
如果您的列只需要日期部分而不需要时间,您应该将列类型更改为 DATE。如果你坚持通过PHP来做,那是一样的,真的:
$today = date('Y-m-d');
$query = mysql_query("
SELECT field FROM table WHERE DATE(column) = '$today'
");
回答by Waqar Alamgir
For date time it is not usefull, instead I try this and working...
对于日期时间,它没有用,而是我尝试这样做并工作......
Today's Visitors!
今天的访客!
sql > select user_id from users where last_visit like concat('%' , CURDATE() , '%');
sql > select user_id from users where last_visit like concat('%' , CURDATE() , '%');
// last_visit coloumn of type 'datetime'
// 'datetime' 类型的 last_visit 列

