I'm using laravel 5.6
and adding a date through dx data grid
, however it is giving a current date which is not what I selected.
I have tried to change the date
format, but in this case, the date is correct. After first insertion it is giving me wrong date (i.e. 1970-01-01
).
$grids = $request->except('_token');
$request->request->set('attendance_date', Carbon::parse($request-
>attendance_date)->format('Y-m-d'));
$grids=array();
$grids['name'] = $request['item']['name'];
$grids['attendance_type'] = $request['item']['attendance_type'];
$grids['dept'] = $request['item']['dept'];
$grids['user_id'] = $request['item']['user_id'];
$grids['attendance_date'] = $request->attendance_date;
$grids['attendance_month'] = $request->attendance_month;
$grids['attendance_year'] = $request->attendance_year;
$grids['org_name'] = $request->org_name;
$grids['org_id'] = $request->org_id;
dd($grids);
$requesteddate = $request->attendance_date;
$requesteduser_id = $grids['user_id'];
$attendances = DB::table('attendances')->where(['user_id' =>
$requesteduser_id])->get()->toArray();
foreach ($attendances as $key => $value) {
$dbattendance_date = $value->attendance_date;
$dbattendance_user_id = $value->user_id;
if ($dbattendance_date == $requesteddate && $dbattendance_user_id ==
$requesteduser_id) {
$insert['status'] = 'error';
$insert['message'] = 'Attendance Already Exits';
return Response()->json($insert);
}
}
$insert = Attendance::create($grids);
return Response()->json($insert);
I solved it by this.
$newattendance_date = $request['item']['attendance_date'];
if(strpos($newattendance_date,' GMT') !== false){
$newattendance_date = substr($newattendance_date,0,strpos($newattendance_date,' GMT'));
$newattendance_date = strtotime($newattendance_date);
$newattendance_date = date('Y-m-d', $newattendance_date);
}
$attendance_date= $newattendance_date;