Php 在列表中查找下一个缺少的日期
我有一个预定日期的列表,存储在这样一个表中Php 在列表中查找下一个缺少的日期,php,laravel,eloquent,Php,Laravel,Eloquent,我有一个预定日期的列表,存储在这样一个表中 1/1/2020 1/2/2020 1/3/2020 1/4/2020 1/6/2020 1/7/200 1/9/2020 我正试图找到第一个错过的日期,以便获得下一次的预订。我有以下代码 $this->inventory->addSelect(['next_available' => InventoryBookingItem::query() ->selectRaw('CASE WHEN date IS NOT NU
1/1/2020
1/2/2020
1/3/2020
1/4/2020
1/6/2020
1/7/200
1/9/2020
我正试图找到第一个错过的日期,以便获得下一次的预订。我有以下代码
$this->inventory->addSelect(['next_available' => InventoryBookingItem::query()
->selectRaw('CASE WHEN date IS NOT NULL THEN DATE_ADD(`date`, INTERVAL 1 DAY) END')
->whereColumn('inventory_id', 'inventories.id')
->orderBy('date', 'desc')
->limit(1)
]);
<>这给了我<代码> 1/10/2020 <代码>,但我也需要考虑表中的空缺。如何从查询中实现这一点?你可以考虑以下例子:
// create period for which you want to check for available dates
$period = new DatePeriod(
new DateTime(),
new DateInterval('P1D'),
new DateTime('+1 month')
);
// your booked dates
$bookedDates = [
'2020-07-16',
'2020-07-17',
'2020-07-19',
'2020-07-21',
'2020-07-26',
'2020-08-01',
];
$dates = array_map(static function (DateTime $date) {
return $date->format('Y-m-d');
}, iterator_to_array($period));
return array_diff($dates, $bookedDates); // it will return all available dates
你可以考虑下面的例子:
// create period for which you want to check for available dates
$period = new DatePeriod(
new DateTime(),
new DateInterval('P1D'),
new DateTime('+1 month')
);
// your booked dates
$bookedDates = [
'2020-07-16',
'2020-07-17',
'2020-07-19',
'2020-07-21',
'2020-07-26',
'2020-08-01',
];
$dates = array_map(static function (DateTime $date) {
return $date->format('Y-m-d');
}, iterator_to_array($period));
return array_diff($dates, $bookedDates); // it will return all available dates