PHPExcel返回一种奇怪的日期格式

时间:2015-03-20 19:29:07

标签: php phpexcel

当我在行/列上使用PHPExcel的getFormattedValue()时,在LibreOffice Calc中,显示为02/19/2015 23:59:40我得到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();

1 个答案:

答案 0 :(得分:1)

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

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

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

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

相关问题