将PHP单元格值转换为date

PHPExcel在同一张图表模板上给我不同的date值格式

这是我使用的代码

$value = $this->worksheet->getCellByColumnAndRow(11, 1) ->getFormattedValue(); $this->_date = DateTime::createFromFormat("j/n/Y",$value); 

我已经尝试了以下的答案,但他们没有解决它

PHPEXCEL获取格式化date,如在Excel文件中可见的那样

如何使用PHPExcel库从excel中获取date

这是我遇到的一个问题,我希望我能帮助也有这个问题的其他人。

您可以直接将Excel序列化的时间戳值转换为PHP DateTime对象,无需创build格式化的string,然后从中创build新的DateTime对象:

 $msExcelSerializedTimestampValue = $this->worksheet ->getCellByColumnAndRow(11, 1) ->getCalculatedValue(); // getValue() is even easier if the cell doesn't contain a formula $this->_date = PHPExcel_Shared_Date::ExcelToPHPObject($msExcelSerializedTimestampValue); 

这为我解决了

 $value = $this->worksheet->getCellByColumnAndRow(11, 1) ->getCalculatedValue(); $timeValue = (string)PHPExcel_Style_NumberFormat::toFormattedString($value,PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2); $this->_date = DateTime::createFromFormat("Ymd",$timeValue);