简体   繁体   English

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

[英]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. 我想做的是做一个SQL查询,它选择两个日期之间的所有委托。

My two dates comes from a datepicker: 我的两个日期来自一个日期选择器:

from 2011-09-20 to 2011-09-25 从2011-09-20到2011-09-25

Now I cant do WHERE date BETWEEN '2011-09-20' AND '2011-09-25' 现在我不能在WHERE date BETWEEN '2011-09-20' AND '2011-09-25'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 ? BETWEEN 2011-09-20 00:00:00 AND 2011-09-25 00:00:00吗?

如果你想要包括25日,你必须去

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. 这是有道理的,因为00:00:00是午夜,并指示一天切换到下一天的那一刻。

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. 您需要添加23:59:59来表示当天的结束,因此它包括当天的所有时间。

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 = $_POST['end_date'] . ' 23:59:59';

Note: Don't forget about SQL Injection . 注意:不要忘记SQL注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM