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

vincent15000's avatar

Issue with PHP Spreadsheet date formatting on Excel

Hello,

I am trying to generate an Excel sheet with dates.

// Test 1
$date = \PhpOffice\PhpSpreadsheet\Shared\Date::formattedPHPToExcel("2022", "12", "31");
...
// Test 2
$time = gmmktime(0, 0, 0, 12, 31, 2022);
$date = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($time);
...
// Format 1
$sheet->getStyle('A1')->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DDMMYYYY);
...
// Format 2
$sheet->getStyle('A1')->getNumberFormat()->setFormatCode("dd/mm/yyyy");

I have tested with test 1 and 2 / format 1 and format 2 and the result is the same : on LibreOffice / OpenOffice, I see 31/12/2022 and on Excel I see 44898.

If you have any idea to solve this issue ... ;).

I know the maatwebsite package which is very good, but the project on which I have to code is not a Laravel projet, and the Excel export is already coded with PHPSpreadSheet but it has that issue.

Thanks a lot for your help.

Vincent

0 likes
1 reply

Please or to participate in this conversation.