首页 > 解决方案 > 如何在两个时间范围内从数据库中检索数据?

问题描述

我想按日期范围和时间范围过滤我的数据。我完成了日期范围,但暂时没有得到解决方案。

我想检索两个时间范围之间的数据。我怎么能那样做?

这是我的日期范围代码:

1) 阿贾克斯

<script>
$(document).ready(function(){  
           $.datepicker.setDefaults({  
                dateFormat: 'yy-mm-dd'   
           });  
           $(function(){  
                $("#from_date").datepicker();  
                $("#to_date").datepicker();  
           });  
           $('#filter').click(function(){  
                var from_date = $('#from_date').val();  
                var to_date = $('#to_date').val();  
                if(from_date != '' && to_date != '')  
                {  
                     $.ajax({  
                          url:"filter.php",  
                          method:"POST",  
                          data:{from_date:from_date, to_date:to_date},  
                          success:function(data)  
                          {  
                               $('#order_table').html(data);  
                          }  
                     });  
                }  
                else  
                {  
                     alert("Please Select Date");  
                }  
           });  
      });  
 </script>

2)SQL查询:

if(isset($_POST["from_date"], $_POST["to_date"]))  {  
  $connect = mysqli_connect("localhost", "root", "", "data");  
  $output = '';  
  $query = "  
       SELECT * FROM date_filter  
       WHERE date BETWEEN '".$_POST["from_date"]."' AND '".$_POST["to_date"]."'  
  ";  
  $result = mysqli_query($connect, $query);  
  $output .= '  
       <table class="table table-bordered">  
            <tr>  
                 <th width="5%">ID</th>  
                 <th width="30%">Customer Name</th>   
                 <th width="12%"> Date</th>  
                 <th width="12%"> Time</th>
            </tr>  
  ';  
  if(mysqli_num_rows($result) > 0)  
  {  
       while($row = mysqli_fetch_array($result))  
       {  
            $output .= '  
                 <tr>  
                      <td>'. $row["id"] .'</td>  
                      <td>'. $row["customer_name"] .'</td>  
                      <td>'. $row["date"] .'</td>  
                      <td>'. $row["time"] .'</td>  
                 </tr>  
            ';  
       }  
  }  
  else  
  {  
       $output .= '  
            <tr>  
                 <td colspan="5">No data Found</td>  
            </tr>  
       ';  
  }  
  $output .= '</table>';  
  echo $output;   }  ?>

帮助我进行时间过滤。从数据库中检索数据。

标签: phpsqlajax

解决方案


推荐阅读