PHPExcel returns 一种奇怪的日期格式

PHPExcel returns a weird date format

当我在 LibreOffice Calc 中显示为 02/19/2015 23:59:40 的行/列上使用 PHPExcel 的 getFormattedValue() 时,我得到 42067.458524537 背部。我如何将其转换为 02/19/2015 23:59:40?

我的PHP代码如下:

<?php
include('/path/to/PHPExcel.php');

$filePath = 'filename.xlsx';

$inputFileType = PHPExcel_IOFactory::identify($filePath);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);

$excel = $objReader->load($filePath);
$worksheet = $excel->getSheet();

echo $worksheet->getCellByColumnAndRow(3, 4)->getFormattedValue();

MS Excel 将日期存储为序列化值,这是一个实数,表示自 1900 年 1 月 1 日或 1904 年 12 月 1 日基准以来的天数,具体取决于电子表格是否创建使用 Windows 1900 日历或 Mac 1904 日历。

序列化值 42067.458524537 对应于 2015 年 3 月 4 日 11:00:17(使用 Windows 1900 日历)

如果你使用了PHPExcel的getFormattedValue()方法,那么它应该根据数字格式将序列化值转换为格式化的date/time字符串应用于该单元格的掩码....假设您在加载电子表格文件时没有设置 loadDataOnly。

如果需要将原始 MS Excel 序列化值转换为 unix 时间戳或 PHP DateTime 对象,则可以使用 PHPExcel_Shared_Date::ExcelToPHP()PHPExcel_Shared_Date::ExcelToPHPObject() 方法;然后使用 DateTime::format() 的原生 PHP date() 功能来格式化它。