PHP Spreadsheet / Laravel Excel - Date format in CSV converts to the users local format after editing

Viewed 385

I'm trying to use Laravel Excel which is based on PHP Spreadsheet to allow users to download a file, edit it and then upload.

I then simply loop through each Cell and compare the value to detect any changes.

However the date format is changing and thus being flagged as a change, even when there is none.

If I export the file as XLS then dates seem to be converted to a timestamp and are really easy to work with e.g.

\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($sheet->getCellByColumnAndRow($col, $row)->getValue())

However if I export as CSV then i'm manually setting the format to Y-m-d (this seems easiest since nobody in the world uses Y-d-m so the format should be clear).

But when I open the CSV on my local machine, edit some non date field and save it, then the dates are then converted to d/m/Y (my local format).

So if I export 2000-12-25 I get back 25/12/2000.

In this specific example obviously I know the format is d/m/Y because it's my local machine, but since this could be used by anyone in the world the cell value could be the date in a huge range of formats and I need to find a way of knowing what format the date is in.

Is there some way to detect the date format from the headers in the CSV file or something like that?

0 Answers
Related