使用 PhpSpreadsheet 读取时间问题

怎么使用 PhpSpreadsheet 读取excel,参考我的另一篇文章博客:使用 PhpSpreadsheet 读取 excel 文件内容

表里的时间格式是这样的

使用 PhpSpreadsheet 读取时间问题

用 PhpSpreadsheet 的 $sheet->getCell('B1')->getValue(); 获取的结果是这样的:
使用 PhpSpreadsheet 读取时间问题

我们可以按下面这样处理一下:

$data = $sheet->getCell('B1')->getValue();
$toTimestamp = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($data);
$data = gmdate("Y-m-d H:i:s", $toTimestamp);

使用 PhpSpreadsheet 读取时间问题

可以看到,成功转出原本格式的时间。然后我们可以继续用 strtotime() 将其转为时间戳格式。
当时,可以的话,最好是策划将其直接配置为时间戳格式。

本作品采用《CC 协议》,转载必须注明作者和本文链接
六月的风
Junwind
讨论数量: 1
public function formatDateTime($excelTimestamp, $format = "Y-m-d H:i:s"){
    return Carbon::instance(Date::excelToDateTimeObject($excelTimestamp))->format($format);
}
2周前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!