如何在我的查询中使日期范围工作

时间:2018-03-14 03:11:07

标签: php jquery

如果我的查询包含AND sales_date BETWEEN '$from' AND '$to',我的查询中如何使日期范围有效。

这是我的代码

  From (<?php echo $from; ?>) To (<?php echo $to; ?>)

这是我的查询。我想在我的查询中使日期范围工作,谢谢

        <?php
          $iq=mysqli_query($conn,"select * from inventory left join product on product.productid=inventory.productid order by inventory_date desc ");
          while($iqrow=mysqli_fetch_array($iq)){


          ?>
            <tr>
              <td class="hidden"></td>
              <td><?php echo date('M d, Y h:i A',strtotime($iqrow['inventory_date'])); ?></td>  
              <td>
              <?php 
                $u=mysqli_query($conn,"select * from `user` left join customer on customer.userid=user.userid left join supplier on supplier.userid=user.userid where user.userid='".$iqrow['userid']."'");
                $urow=mysqli_fetch_array($u);
                if($urow['access']==1){
                  echo "Admin";
                }
                elseif($urow['access']==2){
                  echo $urow['customer_name'];
                }
                else{
                  echo $urow['company_name'];
                }
              ?>
              </td>
              <td align="right"><?php echo $iqrow['action']; ?></td>
              <td align="right"><?php echo $iqrow['product_name']; ?></td>
              <td align="right"><?php echo $iqrow['quantity']; ?></td>
            </tr>
          <?php
 }         }
        ?>

2 个答案:

答案 0 :(得分:0)

您可以在查询中使用BETWEEN

select * from inventory left join product on product.productid=inventory.productid where inventory_date BETWEEN CAST('$startdate' AS DATE) AND CAST('$enddate' AS DATE) order by inventory_date desc

答案 1 :(得分:0)

检查您的inventory_date字段数据类型是否为日期,是否包含日期。

检查$startdate$enddate包含yyyy-mm-dd格式的日期。

$sql = "select * from inventory
            left join product on product.productid=inventory.productid
            where
            date(inventory_date) BETWEEN ".$startdate." AND ".$enddate." order by inventory_date desc";