954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to search between two dates using mysql or php

Hello,

I am doing some search in which my requirment is to search between specific dates.
Like user can put start date and then end date. Once submit then it should find result between these two dates.

Select from table where date1>2001-01-05 And date2< 2001-02-05.

Will this above statement will work. Is there any function which subtracts date like this. How mysql function works when we use query like date less than 2001-01-05

thanks

searchacar
Newbie Poster
17 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

I have not checked your query. But this will work.

$query="select * from table where date_column between "2001-01-05" and "2001-01-10";


This will print all the records where the date_column value lies between the first date and the second date.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
johnsquibb
Junior Poster in Training
84 posts since Nov 2007
Reputation Points: 14
Solved Threads: 14
 

when in doubt check out the mysql manual...

http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html

umm..nowhere in the manual says how to fetch only those records which lies between 2 dates ! :S

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Okay! I checked this and even this works.
Select * from tables where CREATE_TIME > "2008-02-01" and CREATE_TIME < "2008-02-08"

Cheers,
Naveen

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

thanks mate. I think this query will work.

searchacar
Newbie Poster
17 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

You are welcome! :)
Btw, If your problem is solved, you can mark this thread as solved.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

What if I want to query between a date in August and a date in September. I've tried this and it doesn't work:

$query = "SELECT Date FROM test WHERE Date >= '28/08/2010' AND Date <= '02/09/2010'";

Thanks in advance

modesignz
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

If you want to search two dates between php or mysql then there are so many advance facility will be available for searches the dates in php that can provide so many advance facility it into them.

garryamin
Newbie Poster
3 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

SELECT NOW() BETWEEN '1900-1-1' AND '2011-1-1' return 0
SELECT NOW() BETWEEN '1900-1-1' AND '2030-1-1' return 1


http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html

Steelcrusher
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

What if I want to query between a date in August and a date in September. I've tried this and it doesn't work:

$query = "SELECT Date FROM test WHERE Date >= '28/08/2010' AND Date <= '02/09/2010'";

Thanks in advance

from what I know Mysql uses the year-month-day format try sending :

$query = "SELECT Date FROM test WHERE Date >= '2010/08/28' AND Date <= '2010/09/02'";

I tried "SELECT NOW()>= '2012/01/28' AND NOW() <= '2012/03/02'" now was equal to 2012-02-08 10:59:43 and it returned 1

Steelcrusher
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You