您的位置:首页 > 房产 > 家装 > PhpSpreadsheet导入excel时间转换

PhpSpreadsheet导入excel时间转换

2024/10/6 20:29:30 来源:https://blog.csdn.net/weixin_61154364/article/details/139466978  浏览:    关键词:PhpSpreadsheet导入excel时间转换

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

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

环境:thinkphp8、php8、phpspreadsheet 2.0.0

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

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

显示:2024/6/5 11:10

 

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

这种格式: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函数 

<?php
$timeString = "6/5/2024 17:22";
$dateTime = DateTime::createFromFormat('m/d/Y H:i', $timeString);
echo $dateTime->format('Y-m-d H:i:s');

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com