picking dates between two dates.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

picking dates between two dates.

Post by itsmani1 »

i want to fetch all the dates between two dates using a MySql query.
my start date is : 2000-03-03 00:00:00
my end date is : 2000-04-04 00:00:00

and i want to pick all the dates between these two.
any way?

thank you
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Very basically, and if you're working with a single date column in the database...

Code: Select all

select * from myTable where myDateColumn >= '2000-03-03 00:00:00' and  myDateColumn <= '2000-04-04 00:00:00'
If you've got 2 date columns in the database (each record having a start and end date for example) it gets a little more complicated, but it's still pretty easy. If you need code for that then say so and I'll post some.

Obviously all this stuff requires the dates in the database to be some sort of date type (date, datetime, timestamp, etc).
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You could use BETWEEN syntax

Code: Select all

SELECT
  *
FROM
  myTable
WHERE
  WHERE myDateColumn BETWEEN '2000-03-03 00:00:00' AND '2000-04-04 00:00:00'
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply