在sql / php中选择日期和日期(其他格式)之间的所有内容

在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 ?

这是“date”列中的格式: p>

  2011-08-03 13:36:19 
  code>  pre> 
 
 

我想做的是做一个SQL查询,它选择两个日期之间的所有委托。 p >

我的两个日期来自一个日期选择器: p>

2011-09-20至2011-09-25 p>

现在我不能 WHERE date BETWEEN'2011-09-20'''2011-09-25' code> p>

因为列日期还有时间, 如上所示。 p>

那我怎么能这样做? 我可以 BETWEEN 2011-09-20 00:00:00和2011-09-25 00:00:00 code>? p> div>

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'

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.

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.