Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

The Date of Birth does not match the format Y-m-d. in Excel Upload

I am using Laravel-8 and Maatwebsite-3.1 package for excel upload:

imports:

public function onRow(Row $row)
{
    $rowIndex = $row->getIndex();
    if($rowIndex >= 1000)
        return; // Not more than 1000 rows at a time

    $row = $row->toArray();

    $employee_data = [
        'first_name'                        => $row[0],
        'other_name'                        => $row[1] ?? '',
        'last_name'                         => $row[2],
        'email'                             => preg_replace('/\s+/', '', strtolower($row[3])),
        'dob'                               => $this->transformDate($row[4]),
    ];

    $employee = create(Employee::class, $employee_data);
}

public function startRow(): int
{
    return 2;
}

public function customValidationAttributes()
{
    return [
        '0'     => 'First Name',
        '1'     => 'Other Name',
        '2'     => 'Last Name',
        '3'     => 'Email',
        '4'     => 'Date of Birth',
    ];
}

public function rules(): array
{
    return [
        '*.0' => [
            'required',
            'string',
            'max:50'
        ],
        '*.1' => [
            'nullable',
            'string',
            'max:50'
        ],
        '*.2' => [
            'required',
            'string',
            'max:50'
        ],
        '*.3' => [
            'required',
            'email',
            'max:100',
            Rule::unique('employees', 'email')->where(function ($query) {
                return $query->where('company_id', Auth::user()->company_id);
            })
        ],
        '*.4' => [
            'required',
            'date_format:Y-m-d',
           'before:' . Carbon::now()->subYears(18)->format('Y-m-d')
        ],
    ];
}

public function chunkSize(): int
{
    return 1000;
}

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

The date in the excel sheet is like this:

2010-04-01

but when I click on the cell, I got this:

4/1/2010

So when I uploaded I got this error:

The Date of Birth does not match the format Y-m-d.

How do I resolve this?

Thanks

0 likes
1 reply

Please or to participate in this conversation.