日期from_date和to_date之间的Laravel

时间:2018-10-07 06:39:57

标签: php laravel laravel-5.5

我在Laravel工作。

  • 目前,我正在出勤模块中。
  • 我的问题是要离开申请,首先我被要求离开from_dateto_date
  • 接下来,我要在同一日期应用我,直到遇到错误为止。
  • 和中间日期因错误而适用。这两个条件还可以。
  • 但是我申请离开波纹管from_dateto_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();

谢谢

1 个答案:

答案 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();