Karem
Karem

Reputation: 18103

Select all between date and date (other format) in sql / php

This is the format in the column "date":

2011-08-03 13:36:19

What i wish to do is make a sql query where it selects all entrys between two dates.

My two dates comes from a datepicker:

from 2011-09-20 to 2011-09-25

Now I cant do WHERE date BETWEEN '2011-09-20' AND '2011-09-25'

Because there is also time in the column date, as you can see above.

So how can i do this? Can i do BETWEEN 2011-09-20 00:00:00 AND 2011-09-25 00:00:00 ?

Upvotes: 0

Views: 1356

Answers (3)

Jason McCreary
Jason McCreary

Reputation: 72971

Those two conditions are logically equivalent (although your second one has a syntax error as you are missing the single quotes).

You probably mean to add the time to the end date. You'll want to add 23:59:59 to denote the end of the day so it includes all times during that day.

You can do so by appending your end date string (assuming it is POST data from a form with an input name of end_date).

$end_date = $_POST['end_date'] . ' 23:59:59';

Note: Don't forget about SQL Injection.

Upvotes: 0

Jeff Lambert
Jeff Lambert

Reputation: 24661

You answered your own question: zero fill the time. It makes sense, since 00:00:00 is midnight, and indicates the moment one day switches to the next.

Upvotes: 1

Adriano Carneiro
Adriano Carneiro

Reputation: 58595

If you want to include the 25th, you'd have to go

BETWEEN '2011-09-20 00:00:00' AND '2011-09-25 23:59:59'

Upvotes: 1

Related Questions