按月筛选,格式为“Y-m-d”

时间:2017-11-23 05:32:21

标签: php sql laravel eloquent

我的date格式为Y-m-d。我怎样才能按月过滤它?我用January = 1, February = 2之类的数字来指定月份等等。

public function scopegetmonthleavereport($query,$payroll_employee_id,$month)
{

         $query->join('tbl_payroll_leave_employee_v2','tbl_payroll_leave_schedulev2.payroll_leave_employee_id','=','tbl_payroll_leave_employee_v2.payroll_leave_employee_id')
         ->join("tbl_payroll_employee_basic","tbl_payroll_leave_employee_v2.payroll_employee_id","=","tbl_payroll_employee_basic.payroll_employee_id")
         ->join("tbl_payroll_leave_tempv2","tbl_payroll_leave_employee_v2.payroll_leave_temp_id","=","tbl_payroll_leave_tempv2.payroll_leave_temp_id")
         ->select(DB::raw('tbl_payroll_employee_basic.payroll_employee_id , tbl_payroll_leave_schedulev2.payroll_schedule_leave, tbl_payroll_employee_basic.payroll_employee_display_name, tbl_payroll_leave_schedulev2.payroll_leave_temp_with_pay, tbl_payroll_leave_tempv2.payroll_leave_type_id, tbl_payroll_leave_employee_v2.payroll_leave_employee_id, tbl_payroll_leave_schedulev2.consume, tbl_payroll_leave_employee_v2.payroll_leave_temp_hours, sum(tbl_payroll_leave_schedulev2.consume) as total_leave_consume, (tbl_payroll_leave_employee_v2.payroll_leave_temp_hours - sum(tbl_payroll_leave_schedulev2.consume)) as remaining_leave'))
         ->groupBy('tbl_payroll_leave_employee_v2.payroll_leave_temp_id')
         ->where('tbl_payroll_leave_schedulev2.payroll_leave_schedule_archived',0)
         ->where('tbl_payroll_leave_employee_v2.payroll_employee_id', $payroll_employee_id)
         ->whereBetween('tbl_payroll_leave_schedulev2.payroll_schedule_leave', $date);  //where i want to put the date


    return $query;
}

2 个答案:

答案 0 :(得分:1)

尝试和正则表达式在哪里, 使用这样的东西:

->Where('date', 'like', '%-' .$month. '-%')->get();

我没试过。

答案 1 :(得分:0)

use Carbon\Carbon;

public function index(){
        date_default_timezone_set('Asia/Kolkata');

        //Total Students
         $total_students = User::where('role_id','2')->count();         

        $today = Carbon::today()->toDateTimeString();
         //dd($today);
          $first_day = date('Y, n, j', strtotime('-1 day', strtotime($today)));
            $second_day = date('Y, n, j', strtotime('-2 day', strtotime($today)));
            $third_day = date('Y, n, j', strtotime('-3 day', strtotime($today)));
            $fourth_day = date('Y, n, j', strtotime('-4 day', strtotime($today)));
            $fifth_day = date('Y, n, j', strtotime('-5 day', strtotime($today)));
            $sixth_day = date('Y, n, j', strtotime('-6 day', strtotime($today)));
            $seventh_day = date('Y, n, j', strtotime('-7 day', strtotime($today)));     

        return view('home',compact('total_students','total_doner','current_day_student','first_day','second_day','third_day','fourth_day','fifth_day'
                ,'sixth_day','seventh_day'));
    }