匹配MySQL中两个日期列之间的多个日期

时间:2017-02-24 10:01:56

标签: php mysql pdo

我有两列,第1列和第2列。我想在我的查询中匹配这两个日期之间的任何日期。

例如:

1

期间返回此:

$period = new DatePeriod(new DateTime('27-02-2017'), new DateInterval('P1D'), new DateTime('02-03-2017'.'+1 day'));
foreach ($period as $key => $date) {    
           $data=$date->format("d-m-Y");
            //Query SELECT * FROM booking WHERE userid='67' AND ('$data' BETWEEN bookingfrom AND bookingtill)  AND service='test' AND status='0'
}

编辑:找到解决方案:

DatePeriod Object
(
    [start] => DateTime Object
        (
            [date] => 2017-02-24 00:00:00.000000
            [timezone_type] => 3
            [timezone] => UTC
        )

    [current] => 
    [end] => DateTime Object
        (
            [date] => 2017-03-03 00:00:00.000000
            [timezone_type] => 3
            [timezone] => UTC
        )

    [interval] => DateInterval Object
        (
            [y] => 0
            [m] => 0
            [d] => 1
            [h] => 0
            [i] => 0
            [s] => 0
            [weekday] => 0
            [weekday_behavior] => 0
            [first_last_day_of] => 0
            [invert] => 0
            [days] => 
            [special_type] => 0
            [special_amount] => 0
            [have_weekday_relative] => 0
            [have_special_relative] => 0
        )

    [recurrences] => 1
    [include_start_date] => 1
)

3 个答案:

答案 0 :(得分:2)

SELECT * FROM booking WHERE userid='67' AND bookingfrom <= $data AND bookingtill >= $data AND service='test' AND status='0'

答案 1 :(得分:1)

假设从AND bookingtill预订的是DATE字段:

SELECT * 
FROM booking 
WHERE userid='67' 
AND '".date('Y-m-d', strtotime($date))."' BETWEEN bookingfrom AND bookingtill
AND service='test'
AND status='0'

使用“date('Y-m-d', strtotime($date))”设置格式以在MySql中进行比较。

答案 2 :(得分:0)

使用以下查询:

SELECT * FROM booking
    WHERE userid='67' AND $data >= bookingfrom
    AND $data <=bookingtill AND service='test' AND status='0'