Php 两个日期之间的sql查询
form.phpPhp 两个日期之间的sql查询,php,mysql,sql,date,Php,Mysql,Sql,Date,form.php <form action="dropdown.php" method="POST"> <?php $month = array(); for ( $i=1; $i<13; $i++ ) { $month = date('m', mktime(0,0,0,$i,2,2000)); $sel = ( $i == date('n') ? ' selected="selected"' : ''); $options1[] = "
<form action="dropdown.php" method="POST">
<?php
$month = array();
for ( $i=1; $i<13; $i++ ) {
$month = date('m', mktime(0,0,0,$i,2,2000));
$sel = ( $i == date('n') ? ' selected="selected"' : '');
$options1[] = "<option value=\"{$month}\" {$sel}>{$month}</option>";
}
$options_list1 = join("", $options1);
echo "<select name=\"month\" >{$options_list1}</select>";
for ( $j=1; $j<32; $j++ ) {
$theday = date('d', mktime(0,0,0,0,$j,2000));
$sel = ( $j == date('d') ? ' selected="selected"' : '');
$options2[] = "<option value=\"{$theday}\" {$sel}>{$theday}</option>";
}
$options_list2 = join("\r\n", $options2);
echo "<select name=\"day\" >{$options_list2}</select>";
$arrivalyear = array(2013 => "2013",2014 => "2014");
$selected = date("Y");
echo '<select name="year">';
foreach ($arrivalyear as $i => $v) {
echo "<option value=\"" . $i . "\"";
if ($i == $selected) echo " selected=\"selected\"";
echo ">" . $v . "</option>";
}
echo '</select>';
?>
<span> BETWEEN </span>
<?php
$month = array();
for ( $i=1; $i<13; $i++ ) {
$month = date('m', mktime(0,0,0,$i,2,2000));
$sel = ( $i == date('n') ? ' selected="selected"' : '');
$options1[] = "<option value=\"{$month}\" {$sel}>{$month}</option>";
}
$options_list1 = join("", $options1);
echo "<select name=\"month1\" >{$options_list1}</select>";
for ( $j=1; $j<32; $j++ ) {
$theday = date('d', mktime(0,0,0,0,$j,2000));
$sel = ( $j == date('d') ? ' selected="selected"' : '');
$options2[] = "<option value=\"{$theday}\" {$sel}>{$theday}</option>";
}
$options_list2 = join("\r\n", $options2);
echo "<select name=\"day1\" >{$options_list2}</select>";
$arrivalyear = array(2013 => "2013",2014 => "2014");
$selected = date("Y");
echo '<select name="year1">';
foreach ($arrivalyear as $i => $v) {
echo "<option value=\"" . $i . "\"";
if ($i == $selected) echo " selected=\"selected\"";
echo ">" . $v . "</option>";
}
echo '</select>';
?>
<input type="submit" name="sub" value="Date Filter">
</form>
如果有任何解决方案可以找到两个日期之间的日期,并且日期来自db,那么您的查询应该是这样的
select * from mdx where mdx_timestamp <= '2014-02-10' and mdx_timestamp >= '2014-02-08'
从mdx中选择*,其中mdx_时间戳='2014-02-08'
逻辑上不存在大于2014-02-10且小于2014-02-08的日期。您需要反向执行此操作使用
介于
之间的条款:
select * from mdx where mdx_timestamp between '2014-02-08' and '2014-02-10'
或者:
select * from mdx
where mdx_timestamp between
least( '2014-02-08', '2014-02-10' )
and
greatest( '2014-02-08', '2014-02-10' )
参考:使用
Between
子句
从mdx中选择*,其中mdx的时间戳介于“2014-02-08”和“2014-02-10”之间。
我相信您必须将时间添加到日期中,否则它将无法正常工作:
select * from mdx where mdx_timestamp >= '2014-02-10 00:00:00' and mdx_timestamp <= '2014-02-08 23:59:59'
从mdx中选择*,其中mdx\u时间戳>='2014-02-10 00:00:00'和mdx\u时间戳“从mdx中选择*,其中mdx\u时间戳>='2014-02-08'和mdx\u时间戳
select * from mdx where mdx_timestamp >= '2014-02-10 00:00:00' and mdx_timestamp <= '2014-02-08 23:59:59'