将PHP单元格值转换为日期


Converting PHP cell value to date

PHPExcel给我不同的日期值格式在相同的工作表模板

这是我使用的代码

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

我试了下面的答案,但他们没有解决它

PHPEXCEL得到格式化日期,在excel文件中可见

如何使用PHPExcel库从excel获取日期

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

您可以将Excel序列化的时间戳值直接转换为PHP DateTime对象,而无需创建格式化的字符串,然后从中创建一个新的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("Y-m-d",$timeValue);