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

bhhussain's avatar

Excel Date Format

Hi,

I am using the below code to export into excel but the date format is not working.

Date is coming "2020-01-15 14:27:15" like this but I want to make it "15-01-2020" like this

<?php

namespace App\Exports;
use App\Account;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;

use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
//use Maatwebsite\Excel\Concerns\WithMapping;




class AccountExport implements FromCollection,WithHeadings,ShouldAutoSize,WithEvents,WithColumnFormatting
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
       // return Account::all();
       //$arr['accounts'] = Account::where('th_comp_code', auth()->user()->company)->where('th_pay_status', 0)->orderBy('th_tran_no','desc')->get();
       //return view('admin.unpaidbills.index')->with($arr);   
       return Account::select(['th_tran_no','created_at','th_supp_name','th_bill_dt','th_bill_no','th_bill_amt','th_purpose','th_emp_name'])
       ->where('th_comp_code', auth()->user()->company)->where('th_pay_status', 0)
       ->get();
       
       
       
    }

    public function headings(): array
      {
        return [
          'Tran No','Tran Date', 'Supplier Name', 'Bill Date', 'Bill No', 'Bill Amount','Purpose','Emp Name'];
       }

       public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $cellRange = 'A1:H1'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(12);
            },
        ];
    }

    public function columnFormats(): array
    {
        return [
            'B' => NumberFormat::FORMAT_DATE_DDMMYYYY,
            
        ];
    }


       

}

Can any one help me plz?

0 likes
5 replies
skauk's avatar

@bhhussain Check if your date appears in column B or provide any other details about how exactly it is not working.

bhhussain's avatar

@skauk B column is date column and it is showing the date with yyyy-mm-dd hh:mm:ss (2020-01-19 10:13:49)

skauk's avatar

@bhhussain Okay, I think this is because you are operating with a string and not DateTime instance. Try the following:

  • Instead of implementing FromCollection on class, add fromQuery and WithMapping.
  • Rename collection method to query:
public function query()
{
    return Account::query()
        ->where('th_comp_code', auth()->user()->company)
        ->where('th_pay_status', 0);
}
  • Add map method:
public function map($account)
{
    return [
        $account->th_tran_no,
        $account->created_at
    ];
}

Please or to participate in this conversation.