Laravel excel maatwebsite import, date column in excel cell returns as unknown format number. How to solve this?

Viewed 280

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 Answers

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.

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,            
    ]);
Related