Skip to content

Improve DateTime formatting in other locales #298

@mikehaertl

Description

@mikehaertl

This is:

What is the expected behavior?

A cell with a datetime value should be rendered in the correct local format.

So in Germany the a value should be displayed as 20.12.2017 09:15:00 (PHP format string:d.m.Y H:i:s) .

What is the current behavior?

At least in LibreOffice (could not test MS Excel) a datetime value is completely broken. It's displayed as 20.12.y 9:15.

The default FORMAT_DATE_DATETIME format string for datetimes in the Date class is d/m/y h:mm which seems a bit unusual to me. This is a very uncommon format in many locales. It should be DD/MM/YYYY\ HH:MM:SS. This is what I get when I save a file with a full datetime cell. LibreOffice (and presumably also Excel) seem to auto-convert this to the correct local format string.

If this can't be changed, then there should at least be more formatting options provided as Date::FORMAT_* constants to make it easier for international users.

What are the steps to reproduce?

$s = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

$d = new DateTime('2017-12-20 09:15:00');
$excelD = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($d);

$sheet = $s->getActiveSheet();
$sheet->setCellValue('A1', $excelD);
$sheet->getStyle('A1')
    ->getNumberFormat()
    ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME);

Opening this in LibreOffice on a system with German locale leads to the above wrong format.

Which versions of PhpSpreadsheet and PHP are affected?

Development branch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions