Reputation: 750
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);
Upvotes: 0
Views: 1752
Reputation: 750
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;
Upvotes: 2
Reputation: 24579
There is some irrelevant information here in your question, and a few questions I would need to know to be more accurate. That being said, here are a few possibilities I suspect might be occurring:
You have error reporting turned off. There is a wide variety of reasons why you may not get the correct date, and often times with error reporting turned on you will see the reason. For example, your usage of $request->attendance_date
may be returning a null
value, resulting in the parse falling back to the first UTC date possible.
You are doing $request->request->set('attendance_date'
... instead of $request->set('attendance_date'
. Not sure why you are doing this, but later on when you try to use $request->attendance_date
is will by a null
value because it never got set correctly. Again, you would see this problem probably with error reporting/display errrors turned on.
The date passed in your Request
input is wrong. This could be anything from an empty string to an invalid date. A good rule of thumb is if PHP's native strtotime()
function can parse it correctly, then Carbon can too.
Upvotes: 3