如何使用PhpSpreadsheet从表格单元中检索日期? [英] How to retrieve date from table cell using PhpSpreadsheet?

查看:429
本文介绍了如何使用PhpSpreadsheet从表格单元中检索日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有xlsx个表,并且使用PhpSpreadsheet来解析它们.一些单元格格式是日期.问题在于,PhpSpreadsheet以未指定的格式返回日期格式的单元格中的值:

I have xlsx tables and I use PhpSpreadsheet to parse them. Some cells format is date. The problem is that PhpSpreadsheet returns the values from date-formatted cells in an unspecified format:

// 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实例的形式从单元格获取日期?

How to get the date from a cell in form of a UNIX time or a DateTimeInterface instance?

推荐答案

该值是自1900年以来经过的天数.您可以使用PhpSpreadsheet内置函数将其转换为unix时间戳:

The value is amount of days passed since 1900. You can use the PhpSpreadsheet built-in functions to convert it to a unix timestamp:

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

或连接到PHP DateTime对象:

Or to a PHP DateTime object:

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

这篇关于如何使用PhpSpreadsheet从表格单元中检索日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆