如何选择日期范围

时间:2016-11-14 06:55:17

标签: php mysql date

我在mysql数据库中有一个日期范围 2016-11-11 - 2016-11-15 如何选择在日期范围之间以任何方式工作的日期?

到目前为止,代码仅适用于:

11-12,11-13,11-14,11-15,12-15,13-15,14-15,15-15

我的目标是让它与下面的列表一起使用:

12-11,12-12,12-13,12-14

13-11,13-12,13-13,13-14

14-11,14-12,14-13,14-14

有可能吗?

到目前为止,这是我的SQL语句。感谢有人可以帮我解决这个问题。

<?php
    $connect      = mysqli_connect("localhost", "root", "", "database");
    global $connect;   

    if(isset($_POST['Submit']))
    {
        $user_id        = $_POST['user_id'];
        $dtfrom_user    = $_POST['date_from'];
        $dtto_user      = $_POST['date_to'];

        $sql            = "SELECT * FROM table WHERE user_id = '".$user_id."' 

                                                AND 

                                                   date_from BETWEEN CAST('".$date_from."' AS DATE) AND CAST('".$date_to."'   AS DATE) 
                                                OR 
                                                   date_to   BETWEEN CAST('".$date_from."' AS DATE) AND CAST('".$date_to."'   AS DATE)

                                                AND 

                                                   date_to   BETWEEN CAST('".$date_to."'   AS DATE) AND CAST('".$date_from."' AS DATE) 
                                                OR 
                                                   date_from BETWEEN CAST('".$date_to."'   AS DATE) AND CAST('".$date_from."' AS DATE)
                          ";                                

        $run            = mysqli_query($connect, $sql);
        if($run && mysqli_num_rows($run) > 0 )
        {
            while($row = mysqli_fetch_assoc($run))
            {

            }
            echo "Cannot add date";
        mysqli_free_result($run);
        }
        else
        {
            echo "Date added";
            echo mysqli_error($connect);
        }
    }
?>
<form action="date.php" method="post">  
    <table> 
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>User ID : </td>
            <td><input type ="text" name="user_id" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Date from : </td>
            <td><input type ="text" name="date_from" size="30"></td>
        </tr>
                <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Date to : </td>
            <td><input type ="text" name="date_to" size="30"></td>
        </tr>
    </table>    

    <p><input class="btnSuccess" type ="submit" name="Submit" value="Submit"> </p>              
</form>

提前致谢。

0 个答案:

没有答案