doucan1996 2017-12-27 12:42
浏览 238

使用PHPExcel从单元格获取错误的日期

I'm using PHPExcel and trying to get the date values from the cells, but for some reason they give me the wrong date.

$dt_column  = $obj_sheet->getCell('A1');
$unix_date  = PHPExcel_Shared_Date::ExcelToPHP($dt_column);
$date       = gmdate('Y-m-d 00:00:00', $unix_date);

The file is in .xlsx format. The value of the cell, in LibreOffice Calc, is displayed as 1/7/2017, (01/07/2017 on the formula input, don't know if there's any practical difference in handling it on PHPExcel dates).

The returned value given to $dt_column is 42917.

The value attributed to $unix_date is -2208988800.

The value attributed to $date is 2036-02-07 00:00:00.

I tried to manually convert the Excel date values to Unix date values using the formula UNIX_DATE = (EXCEL_DATE - 25569) * 86400, as u'd imagine, same result.

PHPExcel_Shared_Date::$_excelBaseDate is set to CALENDAR_WINDOWS_1900.

How can I get the correct date?

Answered

  • 写回答

1条回答 默认 最新

  • dsv768456 2017-12-27 13:14
    关注

    Excel calculates a date as the addition / substraction of x since 1/1/1900. So your date is 42917 days past 1/1/1900. You can best use that to calculate the correct date.

    Example:

    $d = strtotime("1900-1-1 + ".$dt_column." days");
    

    $d will be the timestamp in UNIX format which can be used with the date() function to format.

    Good luck!

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答