php - sql查询显示2个日期之间的所有行

标签 php mysql

我有一个名为预订详细信息的 SQL 表,其中包含以下列:

Booking_Id
Room_Id
Booking_Date
Check_in_Date
Check_out_Date

我想查看 2 个日期范围内的所有数据行。例如:

$start='2017-04-15'$end='2017-04-25'

sql 查询应仅显示位于这两个日期范围之间的行。

SELECT * FROM booking_details
WHERE '2017-04-15' BETWEEN '2017-04-15' AND '2017-04-25'
OR '2017-04-25' BETWEEN '2017-04-15' AND '2017-04-25'

SELECT * FROM booking_details
WHERE '2017-04-15' BETWEEN '2017-04-15' AND '2017-04-25'

SELECT * FROM booking_details
WHERE '2017-04-15' >='2017-04-15' AND '2017-04-15'<='2017-04-25'

我的编码有问题吗?查看编码并让我知道问题:

<div class="wrapper row3">
<div id="breadcrumb" class="clear"> 
    <header id="header" class="clear">
        <h1><font size="5"><u>VIEW BOOKING - HOTEL DAFFODILS</u></font></h1>
    </header>
    <br>
    <?php
        if(isSet($_SESSION['name']))
        {
            echo "<font color=#737373 size=5> WELCOME ".$_SESSION['name']." !</font><br>";
        }
        echo "<br>";
        if(isSet($_SESSION['email']))
        {
            $a=$_POST["data_1"];
            $b=$_POST["data_2"];
            date_default_timezone_set('Asia/Calcutta');
            $start=date("Y-m-d", strtotime($a));
            $end=date("Y-m-d", strtotime($b));
            $diff = abs(strtotime($end) - strtotime($start));
            $days = $diff/86400;
            $date=date("Y-m-d");

            $_SESSION['start']=$start;
            $_SESSION['end']=$end;
            $_SESSION['date']=$date;
            $_SESSION['days']=$days;

            include 'db_config.php';
            if($start<= $end)
            {
                $sql="SELECT * FROM booking_details WHERE ".$date." >= '".$start."' AND ".$date." <= '".$end."'";
                $result = $conn->query($sql);
                if ($result->num_rows > 0) 
                {
                    // output data of each row
                    echo "<table>
                            <tr>
                                <th>Booking Id </th>
                                <th>Room Id</th>
                                <th>Booking Date</th>
                                <th>Check-in Date</th>
                                <th>Check-out Date</th>
                                <th>No of Adults</th>
                                <th>No of Children</th>
                                <th>Questions/Comments</th>
                                <th>Total_Amount</th>
                                <th>Advance_Amount</th>
                                <th>Due_Amount</th>
                            </tr>";                         
                    while($row = $result->fetch_assoc()) 
                    {
                        echo "<tr>";
                            echo "<td>" . $row['Booking_Id'] . "</td>";
                            echo "<td>" . $row['Room_Id'] . "</td>";        
                            echo "<td>" . $row['Booking_Date'] . "</td>";
                            echo "<td>" . $row['Check_in_Date'] . "</td>";
                            echo "<td>" . $row['Check_out_Date'] . "</td>";
                            echo "<td>" . $row['No_of_Adults'] . "</td>";
                            echo "<td>" . $row['No_of_Children'] . "</td>";
                            echo "<td>" . $row['Questions_Comments'] . "</td>";
                            echo "<td>"."₹" . $row['Total_Amount'] . "</td>";
                            echo "<td>"."₹" . $row['Advance_Amount'] . "</td>";
                            echo "<td>"."₹" . $row['Due_Amount'] . "</td>";
                        echo "</tr>";
                    }
                    echo "</table>";
                }   
            }
            else
            {
                echo "Please enter the correct STARTING or ENDING dates !";
            }
            $conn->close();
        }
        else
        {
            echo "Please LOGIN first !";
        }       
    ?>      
    <form action="admin_view_booking.php">
        <br>
        <div style="padding-bottom: 18px;"><input type="submit" name="back" value="BACK"/></div>
    </form>

</div>

最佳答案

如果您尝试获取开始日期和结束日期之间的预订日期。试试这个。

从 booking_details 中选择 * WHERE Booking_Date BETWEEN $start AND $end

<div class="wrapper row3">
<div id="breadcrumb" class="clear"> 
<header id="header" class="clear">
<h1><font size="5"><u>VIEW BOOKING - HOTEL DAFFODILS</u></font></h1>
</header>
<br>
<?php
    if(isSet($_SESSION['name']))
    {
        echo "<font color=#737373 size=5> WELCOME ".$_SESSION['name']." !</font><br>";
    }
    echo "<br>";
    if(isSet($_SESSION['email']))
    {
        $a=$_POST["data_1"];
        $b=$_POST["data_2"];
        date_default_timezone_set('Asia/Calcutta');
        $start=date("Y-m-d", strtotime($a));
        $end=date("Y-m-d", strtotime($b));
        $diff = abs(strtotime($end) - strtotime($start));
        $days = $diff/86400;
        $date=date("Y-m-d");

        $_SESSION['start']=$start;
        $_SESSION['end']=$end;
        $_SESSION['date']=$date;
        $_SESSION['days']=$days;

        include 'db_config.php';
        if($start<= $end)
        {
            $sql="SELECT * FROM booking_details WHERE ".$date." >= Check_in_Date AND ".$date." <= Check_out_Date";
            $result = $conn->query($sql);
            if ($result->num_rows > 0) 
            {
                // output data of each row
                echo "<table>
                        <tr>
                            <th>Booking Id </th>
                            <th>Room Id</th>
                            <th>Booking Date</th>
                            <th>Check-in Date</th>
                            <th>Check-out Date</th>
                            <th>No of Adults</th>
                            <th>No of Children</th>
                            <th>Questions/Comments</th>
                            <th>Total_Amount</th>
                            <th>Advance_Amount</th>
                            <th>Due_Amount</th>
                        </tr>";                         
                while($row = $result->fetch_assoc()) 
                {
                    echo "<tr>";
                        echo "<td>" . $row['Booking_Id'] . "</td>";
                        echo "<td>" . $row['Room_Id'] . "</td>";        
                        echo "<td>" . $row['Booking_Date'] . "</td>";
                        echo "<td>" . $row['Check_in_Date'] . "</td>";
                        echo "<td>" . $row['Check_out_Date'] . "</td>";
                        echo "<td>" . $row['No_of_Adults'] . "</td>";
                        echo "<td>" . $row['No_of_Children'] . "</td>";
                        echo "<td>" . $row['Questions_Comments'] . "</td>";
                        echo "<td>"."₹" . $row['Total_Amount'] . "</td>";
                        echo "<td>"."₹" . $row['Advance_Amount'] . "</td>";
                        echo "<td>"."₹" . $row['Due_Amount'] . "</td>";
                    echo "</tr>";
                }
                echo "</table>";
            }   
        }
        else
        {
            echo "Please enter the correct STARTING or ENDING dates !";
        }
        $conn->close();
    }
    else
    {
        echo "Please LOGIN first !";
    }       
?>      
<form action="admin_view_booking.php">
    <br>
    <div style="padding-bottom: 18px;"><input type="submit" name="back" value="BACK"/></div>
</form>

关于php - sql查询显示2个日期之间的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43462703/

相关文章:

php - 在php中解析xml

Mysql:错误代码:1452。无法添加或更新子行:外键约束失败

sql - MySQL:如何计算特定日期后的周数?

mysql - 优化 SQL 子查询进行统计

来自 PHP 的 JavaScript(带有 Ajax)和输出缓冲

php - array_shift 如何在 while 循环内工作

mysql - 从存储过程返回一个结果集

python - 在 mysql/python 中转义整个字符串

javascript - 图片前面的自定义文字

php - 更改循环中的最后一项输出 (ACF)