使用PHP Excel进行Excel日期转换 [英] Excel date conversion using PHP Excel

查看:595
本文介绍了使用PHP Excel进行Excel日期转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Excel中读取日期,其格式为12/5/2012天/月/年 使用此代码进行读取.使用PHP EXCEL

i am reading date from excel which is in this format 12/5/2012 day/month/year using this code to read . using PHP EXCEL

   PHPExcel_Style_NumberFormat::toFormattedString($value['A'],'YYYY-MM-DD' );

其工作方式就像魅力一样,将上述日期"2012年12月5日"转换为"2012-12-05年"

its working like charm converting the above date '12/5/2012' to '2012-12-05'

现在的问题是,如果日期让我们说18/5/2012,或者您可以说如果我将日期设置为大于12,那么在格式化

now the problem is if the date is lets says 18/5/2012 or you can say if i set day greater than 12 it gives me this date 18/5/2012 in this format 18/5/2012 after formating

我也尝试过这个东西

      $temp  = strtotime(  PHPExcel_Style_NumberFormat::toFormattedString($value['A'],'YYYY-MM-DD' );
      $actualdate = date('Y-m-d',$temp) ;

THis还将正确转换日期'12/5/2012',但在这种情况下,2012年5月18日的输出为1970-01-01

THis is also converting the date '12/5/2012' correct but in this case 18/5/2012 it gives output as 1970-01-01

推荐答案

请使用此公式将Excel日期更改为Unix日期,然后可以使用"gmdate"在PHP中获取实际日期:

Please use this formula to change from Excel date to Unix date, then you can use "gmdate" to get the real date in PHP:

UNIX_DATE = (EXCEL_DATE - 25569) * 86400

并从Unix日期转换为Excel日期,请使用以下公式:

and to convert from Unix date to Excel date, use this formula:

EXCEL_DATE = 25569 + (UNIX_DATE / 86400)

将此公式放入变量后,您可以使用以下示例获取PHP中的实际日期:

After putting this formula into a variable, you can get the real date in PHP using this example:

$UNIX_DATE = ($EXCEL_DATE - 25569) * 86400;
echo gmdate("d-m-Y H:i:s", $UNIX_DATE);

这篇关于使用PHP Excel进行Excel日期转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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