A getValue()
call on a field containing a date should return a value like 41959.00
if that field really does contain an MS Excel date value.... that is, an MS Excel serialized datetime stamp based on the number of days since 1st January 1900 (or 1st January 1904 if the file was created using the Mac version of MS Excel)
To get a formatted date string, you need to call getFormattedValue()
instead; and PHPExcel then uses the number format mask for that cell to format the date according to that mask.
To identify if a cell contains an MS serialized datetime stamp, you can use a call to PHPExcel_Shared_Date::isDateTime()
first.
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
echo 'Worksheet - ' , $worksheet->getTitle() , EOL;
foreach ($worksheet->getRowIterator() as $row) {
echo ' Row number - ' , $row->getRowIndex() , EOL;
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
foreach ($cellIterator as $cell) {
if (!is_null($cell)) {
echo ' Cell - ' , $cell->getCoordinate() , ' - ';
if (PHPExcel_Shared_Date::isDateTime($cell)) {
echo $cell->getFormattedValue() , EOL;
} else {
echo $cell->getValue() , EOL;
}
}
}
}
}
Rather than returning a formatted data value, you can also ask PHPExcel to return the date as a Unix timestamp, or as a PHP DateTime object instead; and then you'll be able to format it however you want using PHP's built-in date functions or DateTime methods.
if (PHPExcel_Shared_Date::isDateTime($cell)) {
$unixTimeStamp = PHPExcel_Shared_Date::ExcelToPHP($cell->getValue());
echo date('d-M-Y H:i:s', $unixTimeStamp), PHP_EOL;
}
or
if (PHPExcel_Shared_Date::isDateTime($cell)) {
$dateTimeObject = PHPExcel_Shared_Date::ExcelToPHPObject($cell->getValue());
echo $dateTimeObject->format('d-M-Y H:i:s'), PHP_EOL;
}