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

Josadec's avatar

Laravel Excel Format Date Datetime

Good afternoon,

I have this situation when I export to Excel data with column format, I using "use Maatwebsite\Excel\Concerns\WithColumnFormatting;" but when I open the excel file I saw the columns start with "d/m/Y H:i" but I need "m/d/Y H:i" Excel format. somebody could help with this?

Export File

<?php

namespace App\Exports;

use App\Models\Project;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class ProjectsExport implements FromCollection,
WithMapping,
WithColumnFormatting,
WithHeadings,
ShouldAutoSize,
WithEvents
{
    /**
    * @return \Illuminate\Support\Collection
    */

    use Exportable;

    private $filters;

    public function __construct($filters)
    {
        $this->filters = $filters;
    }

    public function map($project):array
    {
        return [
            $project->estimator,
            $project->name,
            $project->department,
            /* Carbon::parse($project->requested_date)->format('m/d/Y H:i'), */
            Date::dateTimeToExcel(Carbon::parse($project->requested_date)),
            Date::dateTimeToExcel(Carbon::parse($project->returned_date)),
            $project->turn_around,
            $project->takeoff_time,
            $project->value,
            $project->state->name,
            $project->type_quote->name,
            $project->return_days,
            $project->return_hours,
            $project->return_humans,
            $project->notes
        ];
    }

    public function headings(): array
    {
        return[
            'Estimator',
            'Project Name',
            'Department',
            'Requested Date',
            'Returned Date',
            'Turn Around',
            'TakeOff Time',
            'Value',
            'State Name',
            'Type Of Quote',
            'Return Days',
            'Return Hours',
            'Return Humans',
            'Notes'
        ];    
    }

    public function columnFormats(): array
    {
        return[
            'D' => NumberFormat::FORMAT_DATE_DATETIME,
            'E' => NumberFormat::FORMAT_DATE_DATETIME
        ];
    }

   public function registerEvents(): array
   {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                $event->sheet->getStyle('A1:N1')->applyFromArray([
                    'font' => ['bold' => true]
                ]);
            }
        ];
   }

    public function collection()
    {
        return Project::filter($this->filters)->get();
    }
}

0 likes
2 replies
vdvcoder's avatar

Try with this:

Carbon\Carbon::parse('12/01/2022 10:10')->format('m/d/Y H:i');

Result: => "12/01/2022 10:10"

Carbon\Carbon::parse($project->requested_date)->format('m/d/Y H:i');

Josadec's avatar

@vdvcoder Thank you, I know change the format in laravel but in this case I tried to export to excel with format date-time not as string

Please or to participate in this conversation.