find missing dates from date range

后端 未结 6 2023
小蘑菇
小蘑菇 2020-12-16 05:49

I have query regarding get the dates which are not exists in database table.

I have below dates in database.

2013-08-02
2013-08-02
2013-08-02
2013-08         


        
6条回答
  •  执笔经年
    2020-12-16 06:39

    This is how i would do it:

    $db_dates = array (
    '2013-08-02',
    '2013-08-03',
    '2013-08-05',
    '2013-08-08',
    '2013-08-09',
    '2013-08-10',
    '2013-08-13'
    );
    $missing = array();
    $month = "08";
    $year = "2013";
    $day_start = 1;
    $day_end = 14
    for ($i=$day_start; $i<$day_end; $i++) {
        $day = $i;
        if ($i<10) {
            $day = "0".$i;  
        }
        $check_date = $year."-".$month."-".$day;
        if (!in_array($check_date, $db_dates)) {
            array_push($missing, $check_date);  
        }
    }
    print_r($missing);
    

    I made it just to that interval but you can just define another interval or make it work for the whole year.

提交回复
热议问题