PhpSpreadsheet导入excel时间转换

作者 : admin 本文共424个字,预计阅读时间需要2分钟 发布时间: 2024-06-5 共2人阅读

问题:PhpSpreadsheet导入excel表格时间数据不一样

背景:导入有一个字段需要输入时间、但是excel的时间格式不一样

环境:thinkphp8、php8、phpspreadsheet 2.0.0

我是将完整的时间字符串复制进去 

输入:2024-06-05 11:10:59

显示:2024/6/5 11:10

 

PhpSpreadsheet导入excel时间转换插图

再下一步到php里面之后变成了

PhpSpreadsheet导入excel时间转换插图(1)

这种格式:6/5/2024 17:22

所以我们为了正常插入数据库要转换为正常的时间

有两种方法

一、date函数

<?php
$timeString = "6/5/2024 17:22";
$timestamp = strtotime($timeString); // 将字符串解析为时间戳
$formattedDate = date('Y-m-d H:i:s', $timestamp); // 格式化时间戳为所需格式
echo $formattedDate;

二、DateTime函数 

format('Y-m-d H:i:s');

PhpSpreadsheet导入excel时间转换插图(2)

本站无任何商业行为
个人在线分享 » PhpSpreadsheet导入excel时间转换
E-->