5

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.

2
  • if you got your answer you should accept the answer buddy. its just not like you come, ask a question, get your answer and run away.. Commented May 17, 2013 at 19:43
  • sorry new to this forum, dont know where is the answer buddy, ive been looking for it but cant find it. can you point me to it? Commented May 20, 2013 at 13:34

5 Answers 5

4

You should use Now() to get the current date of the system.

Sign up to request clarification or add additional context in comments.

1 Comment

NOW() gives current date and time. CURRENT_DATE() gives the current date.
2

See documentation here.
Use BETWEEN
mysql documentation here

Comments

1

use between

mysql_query("SELECT * FROM location WHERE CURDATE() between from_date and to_date 

Comments

0

Try NOW() insted of DATE()

    mysql_query("SELECT * FROM location WHERE from_date >= NOW() AND to_date <= NOW()")

Get more about NOW() here

1 Comment

Thanks everyone, i went with mysql_query("SELECT * FROM location WHERE CURDATE() BETWEEN from_date AND to_date") and it worked great.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.