1

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);

enter image description here

Uddyan Semwal
  • 564
  • 1
  • 6
  • 20

2 Answers2

2

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:

Possibility #1

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.

Possibility #2

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.

Possibility #3

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.

Community
  • 1
  • 1
Jeremy Harris
  • 23,007
  • 13
  • 73
  • 124
2

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;
Uddyan Semwal
  • 564
  • 1
  • 6
  • 20