我在Laravel工作。
from_date
到to_date
。from_date
和to_date
之后。这种情况
不被如何计算此条件。我下面的代码请检查
$keyWithData = DB::table('leave_allocations')
->select('employee','leave_type','name')
->where('leave_allocations.from_date','<=',$dateS->format('Y-m-d')." 00:00:00")
->where('leave_allocations.to_date','>=',$dateS->format('Y-m-d')." 00:00:00")
->where('leave_allocations.from_date','<=',$dateE)
->where('leave_allocations.to_date','>=',$dateE)
->where('leave_allocations.employee',$empdata->name)
->where('leave_allocations.leave_type',$type)
->first();
谢谢
答案 0 :(得分:0)
我认为您只需要whereBetween()
和orWhereBetween()
$keyWithData = DB::table('leave_allocations')
->select('employee','leave_type','name')
->where(function($query) use($dateS, $dateE) {
$query->whereBetween('from_date', [
$dateS->format('m-d-Y 00:00:00'),
$dateE->format('m-d-Y 00:00:00')
])
->orWhereBetween('to_date', [
$dateS->format('m-d-Y 00:00:00'),
$dateE->format('m-d-Y 00:00:00')
]);
})
->where([
['leave_allocations.employee',$empdata->name].
['leave_allocations.leave_type',$type]
])
->first();
使用日期范围的另一种方法
// create a period object between new leaves start date and end date.
$period = new \DatePeriod(
$dateS,
new \DateInterval('P1D'),
$dateE
);
// all the days between new leave start date and end date.
$new_leave_dates = [];
foreach ($period as $key => $value) {
array_push($dates, $value->format('Y-m-d'));
}
// get all the rows without those days.
$keyWithData = DB::table('leave_allocations')
->select('employee','leave_type','name')
->whereNotIn('from_date', $new_leave_dates)
->where([
['leave_allocations.employee',$empdata->name].
['leave_allocations.leave_type',$type]
])
->first();