【问题标题】:laravel between dates from_date and to_date日期 from_date 和 to_date 之间的 laravel
【发布时间】:2018-10-07 06:39:56
【问题描述】:

我在 Laravel 工作。

  • 目前,我在考勤模块工作。
  • 我的问题是离职申请,先申请离职from_dateto_date
  • 接下来,我将通过错误申请相同的日期。
  • 和中间日期因错误而应用。这两个条件OK。
  • 但我申请在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();

谢谢

【问题讨论】:

    标签: php laravel laravel-5.5


    【解决方案1】:

    我想你只需要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();
    

    【讨论】:

    • 这没关系。但是如何在日期之前和之后工作。
    • 假设我在 2018 年 1 月 10 日至 2018 年 10 月 31 日第一次申请休假
    • 接下来我在 2018 年 9 月 29 日至 2018 年 1 月 11 日申请休假。此日期为错误。您的情况在此日期无效
    • 超过日期中间日期相等如何工作请解释。
    • 哦,我明白了,用另一种方法很容易实现,我会更新答案
    猜你喜欢
    • 2023-03-24
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多