2017-06-01 82 views
1

我有xlsx表,我使用PhpSpreadsheet来解析它们。有些单元格格式是日期。问题是,PhpSpreadsheet返回从日期格式的单元格的值在未指定的格式:如何使用PhpSpreadsheet从表格单元格中检索日期?

// How it looks in excel: 2017.04.08 0:00 
$value = $worksheet->getCell('A1')->getValue(); // 42833 - doesn't look like a UNIX time 

如何从一个单元格中日期的UNIX时间或DateTimeInterface实例的形式?

回答

4

值是几天过去了自1900年以来,您可以使用内置的功能PhpSpreadsheet将其转换为Unix时间戳量:

$value = $worksheet->getCell('A1')->getValue(); 
$date = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value); 

或一个PHP DateTime对象:

$value = $worksheet->getCell('A1')->getValue(); 
$date = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value); 
相关问题