2

how to solve date format?

Error:

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '43112'

My import class

 public function model(array $row)
{
    return new Agreement([
        'code' => $row[0],
        'start_date' => $row[1], 
        'end_date' =>$row[2],            
    ]);
}

2 Answers2

2

The datetime format is wrong, as the error says. It would be helpful to see what format you tried to use an input, but generally speaking the default format for date in mysql is Y-m-d, and for datetime it is Y-m-d H:i:s.

Try changing your format to comply with either of stated two formats and see what happens.

Dharman
  • 30,962
  • 25
  • 85
  • 135
R_Dax
  • 706
  • 3
  • 10
  • 25
2

Convert date to mysql format using Carbon

return new Agreement([
        'code' => $row[0],
        'start_date' => Carbon::createFromFormat("d-M-Y",$row[1])->format("Y-m-d"), 
        'end_date' =>Carbon::createFromFormat("d-M-Y",$row[2])->format("Y-m-d"),            
    ]);

Also import Carbon

use Carbon\Carbon;

Updated If it has null value in excel then

return new Agreement([
        'code' => $row[0],
        'start_date' =>!empty($row[1])?Carbon::createFromFormat("d-M-Y",$row[1])->format("Y-m-d"):null, 
        'end_date' =>!empty($row[2])?Carbon::createFromFormat("d-M-Y",$row[2])->format("Y-m-d"):null,            
    ]);
John Lobo
  • 14,355
  • 2
  • 10
  • 20