Skip to content

Formatting a date in Excel sheet #3460

Answered by MarkBaker
TonioIncredible asked this question in Q&A
Discussion options

You must be logged in to vote

If you are generating Excel files using the Default Value Binder, then you need to set the cell values to an Excel serialized DateTime value yourself, using the functions available in \PhpOffice\PhpSpreadsheet\Shared\Date, as described in the documentation. And then you can set the Number Format Mask to display the value however you choose.

e.g.

$date = new DateTimeImmutable('2023-03-16');
$spreadsheet->getActiveSheet()
    ->setCellValue('A2', Date::PHPToExcel($date));
$spreadsheet->getActiveSheet()->getStyle('A2')
    ->getNumberFormat()
    ->setFormatCode('yyyy-mm-dd');

or

$date = '2023-03-16';
$spreadsheet->getActiveSheet()
    ->setCellValue('A2', Date::PHPToExcel($date));
$spreadsheet

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@TonioIncredible
Comment options

Answer selected by oleibman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants