I am trying to import a excel file using PhpExcel lib
for all other fields the getValue()
function works but when it encounters a field with format date as set in ms-excel2013
the date field in exel file is in format d-m-Y like 16-11-2014
but when I try to import it's value the getValue()
returns 11-16-14
which when passed to strtotime
further returns false
in turn causing the date('Y-m-d',strtotime($date))
to return 1970-01-01
.
I searched whole of web and stackoverflow but none solution fixed my problem.
In excel file i see the date as 16-11-2014
and want it to be imported as is.
Here's the code
protected function importExcel($filePath) {
$excelData = array();
if ($filePath) {
$objPHPExcel = PHPExcel_IOFactory::load($filePath);
$objPHPExcel->setReadDataOnly(true);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
$data = array();
for ($row = 1; $row <= $highestRow; ++$row) {
$values = array();
for ($col = 0; $col < $highestColumnIndex; ++$col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
if (PHPExcel_Shared_Date::isDateTime($cell))
throw new Exception("is date time"); // just a check
$val = $cell->getValue();
if (isset($val) && $val)
$data[$row][$col] = $val;
}
}
$excelData[$worksheetTitle] = $data;
}
return $excelData;
}
return FALSE;
}