700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > php导入excel 日期 使用PHPExcel导入excel数据时 日期错误的问题

php导入excel 日期 使用PHPExcel导入excel数据时 日期错误的问题

时间:2022-08-13 20:40:56

相关推荐

php导入excel 日期 使用PHPExcel导入excel数据时 日期错误的问题

通过PHPExcel将excel表格的数据导入数据库时,如果表格中存在日期单元格式,这个单元数据读到php中会变成一串数字,

因此我们在读取的时候,需要将日期进行格式化:functionget_date_by_excel($date)

{

if(!$date||$date=='0000-00-00')returnnull;

$unix_time=\PHPExcel_Shared_Date::ExcelToPHP($date);

return($unix_time

}

使用此封装好的函数,对日期单元进行格式化转换:

如读取的日期单元为变量$creatdate

那么

$creatdate = get_date_by_excel(creatdate);

即可转换成正常的日期格式。

-----------------------------------

下面是网上的几个封装好的相关方法,记录一下,以便往后有需要用到/**

*判断字符串是否是日期格式

*@param$date

*@param$format

*@returnbool

*/

functionis_date($date,$format='Y-m-d')

{

if(!$date||$date=='0000-00-00')returnfalse;

$unix_time_1=strtotime($date);

if(!is_numeric($unix_time_1))returnfalse;//非数字格式

$format_date=date($format,$unix_time_1);

$unix_time_2=strtotime($format_date);

return($unix_time_1==$unix_time_2);

}

/**

*excel数据导入日期格式化

*@param$date

*@returnfalse|string

*/

functionget_date_by_excel($date)

{

if(!$date||$date=='0000-00-00')returnnull;

$unix_time=\PHPExcel_Shared_Date::ExcelToPHP($date);

return($unix_time

}

/**

*获取excel日期格式化结果

*@param$datestringexcel日期单元格字符串

*@param$defaultstring$date未非日期时返回默认日期

*@returnstring

*/

functionexcel_date_format($date,$default='')

{

if($default=='')$default=date('Y-m-d');

if(is_date($date))return$date;

returnget_date_by_excel($date)?:$default;

}

本文地址:/post/120.html 转载请注明来源

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。