excellaravel-9maatwebsite-excel

Laravel - How to convert date value of value in a cell on imported excel files


I tried to make a function where User could import formatted file such as .csv or .xlsx. I make the function using Maatwebsite excel library.

When I tried to insert the data to database, a cell that has value of today date 24/01/2023 is converted to 44950

My Import model kinda like this

public function model(array $row)
{
    return new Event([
        /**
    * Other attributes and columns
    *
    */
        'date' => $row[1],
    ]);
}

How to convert those value to 'Y-m-d'?


Solution

  • I found the best solutions, idk what happened to the excel imported files, but this helps me.

    use PhpOffice\PhpSpreadsheet\Shared\Date;
    
    public function model(array $row)
    {
        return new Event([
            /**
        * Other attributes and columns
        *
        */
            'date' => Date::excelToDateTimeObject($row[1]),
        ]);
    }
    

    Source: https://github.com/SpartnerNL/Laravel-Excel/issues/1978