通过选择两个不同的日期,从一个表中获取一个月的记录

时间:2014-12-01 13:07:11

标签: php mysql

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    if( isset( $_POST['date_submit'] ) ) {
        if(isset($_POST['datepicker_from']) && isset($_POST['datepicker_to']))
        {<br/>
            $date_from=$_POST['datepicker_from'];
            $date_to=$_POST['datepicker_to'];
            echo $date_to;
            echo $date_from;
            #$where_query="WHERE order_date >= ".$date_from." AND order_date <= ".$date_to;
        }
        $query="SELECT * FROM invoice WHERE order_date >= ".$date_from." AND order_date <= ".$date_to;
        $result_for_search=mysql_query($query, $con);
        #echo $result_for_search;
        if( $result_for_search ) { echo 'search query for datepicker excuted successfully'; }

        while($row=mysql_fetch_array( $result_for_search ) ) {
            echo $row['id'] . ' ' . $row['customer'] . ' ' . $row['products'] . ' ' . <br/>$row['qty'] .
             ' ' . $row['unit_price'] . ' ' . $row['total_cash'] . ' ' . <br/>$row['payment_type'] . 
             ' ' . $row['order_date'] . ' ' . $row['discount'];
        }
    }
}

我使用javascript datepicker()方法选择两个日期,当选择了两个日期时,我想要针对这两个选定日期进行记录。日期是我的发票表的order_date属性。

我也使用了BETWEEN。但它没有奏效。

1 个答案:

答案 0 :(得分:0)

假设您的输入格式为mm / dd / yyyy。

您需要使用date()strtotime转换为mysql格式。

    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

           $datepicker = '';

    if( isset( $_POST['date_submit'] ) ) {

        if(isset($_POST['datepicker_from']) && isset($_POST['datepicker_to']))
        {
            $date_from = date("Y-m-d", strtotime($_POST['datepicker_from']));
            $date_to   = date("Y-m-d", strtotime($_POST['datepicker_to']));

        $datepicker = "WHERE order_date >= '$date_from' AND order_date <= '$date_to'";  

    }

            $query="SELECT * FROM invoice $datepicker";
            $result_for_search=mysql_query($query, $con);

        if( $result_for_search ) {

            while($row=mysql_fetch_array( $result_for_search ) ) {

            echo    $row['id'] . ' ' . $row['customer'] . ' ' . $row['products'] . '<br/> ' . 
                    $row['qty'] .' ' . $row['unit_price'] . ' ' . $row['total_cash'] . '<br/> ' . 
                    $row['payment_type'] . ' ' . $row['order_date'] . ' ' . $row['discount'];
            }

    }
    }
}
相关问题