isDateTime returns true if you have a date (24/11/2012) an hour (16:10:00), or a date/time (24/11/2012 16:10:00) and variants of this (examples in my locales).
Therefore, it is not sufficient, except as a pre-filter: my value is a temporal data or not?If not, write it in text, ignore it... Your way.
If yes... One way to proceed is to read the format and compare it to known formats to act accordingly.
To determine these known formats, that are not necessarily what you might think, the easiest is the rule of thumb: you display relevant cells of one of your files format.
How to display them? Like this:
echo $objPHPExcel->getActiveSheet()->getStyle ('A1')->getNumberFormat()->getFormatCode();
Adapts the coordinates of the cell as needed.
Note: the PHPExcel_Shared_Date class provides two static methods that convert an Excel time value to its equivalent php: ExcelToPHP and ExcelToPHPObject.
This can avoid a step : ExcelValue->String->timestamp/object => ExcelValue->Timestamp/object. But, if you don't make tests on the timestamp/object, just doing a date_format/DateTime::Format to have a string for MySQL,
take the short way : ExcelValue->MySQLString.
Your code snippet:
//Why doesn't next lines not working? I should like that this construction works because then I'm using the formatting of PHPExcel
//$data = $cell-> getFormattedValue()-> setFormatCode (PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2).'
';
Try to make a:
var_dump($cell->getFormattedValue());
in the same place or not far away. The dump gives you something that looks like an object?I don't think so.
And the logical consequence: when you are trying to call a method, php screams.
setFormatCode is part of the Style_NumberFormat class, which is accessed by the class Style, collection of Style are in worksheet.A cell has no property that contains (or a method that returns) an object of this class, you must use the Worksheet object, possibly indirectly ($cell->getParent()->getStyle($cell->getCoordinate())->getNumberFormat()->setFormatCode ('my_code')).
However, setFormatCode allows you to change the format of a cell, I'm not sure this is the desired goal.