I am trying to read spreadsheet documents using PHPSpreadsheet, I have a problem reading dates in the sheets, I want to read them in the way it is formatted, the dates are in YYYY-MM-DD format using cell number formatting, when I save the sheets in ods and xls formats I get incorrect date format and when I save the sheets in xlsx format the date format is correct, below is the code that I am using to read spreadsheets
$PHPSpreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file);
foreach ($PHPSpreadsheet->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
foreach ($cellIterator as $cell) {
if (!is_null($cell)) {
if (\PhpOffice\PhpSpreadsheet\Shared\Date::isDateTime($cell)) {
echo $cell->getStyle()->getNumberFormat()->getFormatCode();
echo '<br>';
echo $cell->getValue();
echo '<br>';
echo $cell->getCalculatedValue();
echo '<br>';
echo $cell->getFormattedValue();
echo '<br>';
}
}
}
}
}
this is the output that I get for xlsx, ods, xls respectively
YYYY\-MM\-DD
43196
43196
2018-04-06
d-mmm-yy h:mm:ss
43196.041666667
43196.041666667
6-Apr-18 1:00:00
d-mmm-yy h:mm:ss
43196.041666667
43196.041666667
6-Apr-18 1:00:00
The output that I get while saving in xlsx format is the correct format in the sheets, ods is the format that I will be using the most, am I doing anything wrong or is it a limitaion of PHPSpreadsheet.