Laravel Excel Import Date Format Issue (Solved)

Hello Artisan

In this laravel excel import date format error solving example, i will show you that how we can solve Laravel excel maatwebsite 3.1 import, date column in excel cell returns as unknown format number.

Today i was facing an error that is Laravel Excel is converting dates from heading into some numbers. By using Maatwebsite/Laravel-Excel version 3.1 to import excel sheet, here I faced an issue date time column of the excel sheet returns unknown number.

Example : Assume Cell value "29/07/1989" and it returns as "32178" when import.

Solution: Create a helper method like below:

public function transformDate($value, $format = 'Y-m-d')
{
    try {
        return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value));
    } catch (\ErrorException $e) {
        return \Carbon\Carbon::createFromFormat($format, $value);
    }
}

 

And bow use it like that

public function model(array $row)
{
  return new user([
      'name' => $row[0],
      'email' => $row[1],
      'birth-date' => $this->transformDate($row[2]),
  ]);
}

 

Hope it can help you.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js