Reputation: 85
i am trying to create a MySQL query that will take current date and compare it to the 2 date fields that i have in my table and return the rows that satisfy that query.
here is my columns 1- [from_date] which type is Date 2- [to_date] which also holds type Date
the query suppose to return the rows that falls in between those dates.
here is my query
mysql_query("SELECT * FROM location WHERE from_date >= DATE() AND to_date <= DATE()")
my problem is that it doesn't return anything. should i switch my column type to DATETIME?
Thanks in advance.
Upvotes: 5
Views: 3085
Reputation: 74106
The DATE()
function actually just extracts the date part from a date or datetime expression. It doesn't, however, not return the current date as you intend.
To get the current time use NOW()
or CURDATE()
:
mysql_query("SELECT * FROM location WHERE CURDATE() BETWEEN from_date AND to_date")
If trying to find a something between two values, MySQL also has the BETWEEN
operator
Upvotes: 0
Reputation: 35
use between
mysql_query("SELECT * FROM location WHERE CURDATE() between from_date and to_date
Upvotes: 1