PHPExcel detect if the cell value is formated to date or not

itsmecidz picture itsmecidz · Feb 12, 2015 · Viewed 12.5k times · Source

Good day guys i have a problem on how to detect if the cell value is formated to date or not when i been upload a date value i use this code

$data = $objWorksheet->getCellByColumnAndRow($col, $row);

when a cell value is formated into date the value is $data = 27474 when is not $data = 12/2/1983 then i use this code to format their date

if(PHPExcel_Shared_Date::isDateTime($data)){
           $cellValue = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
           $dateValue = PHPExcel_Shared_Date::ExcelToPHP($cellValue);                        
           $dob =  date('Y-m-d',$dateValue);    
    }                               

now, the problem is when $data is in formated on date this code is OK but when $data is not formated the value is $data = 2036-02-18 which is wrong value

can anyone help me how to resolve this problem Thanks in Advance

Answer

Isanka Wijerathne picture Isanka Wijerathne · Feb 12, 2015

Please try this, this should work,

if(PHPExcel_Shared_Date::isDateTime($data)){
   $cellValue = $objWorksheet->getCellByColumnAndRow($col, $row);
   $InvDate= $cellValue->getValue();

   if(PHPExcel_Shared_Date::isDateTime($cell)) {
   $InvDate = date($format, PHPExcel_Shared_Date::ExcelToPHP($InvDate)); 
}  
}