dongyi5570 2016-12-29 09:33
浏览 36

PHP:将日期数据从Excel设置为数组作为值

I have a system that developed using Yii2 framework, and I've an Excel file that consist a date value imported to the system, for example:

ExcelFile

-------------------------------------------------
    Name   |   Birthdate   |    Place of Birth
-------------------------------------------------
   Archie  |  1995-09-18   |        Nevada
   Gerry   |  1989-01-23   |        Gotham

I'm using PHPExcel for reading the data to PHP, and then the data set to new array, for example

Array

Array
(
    [0] => Array
        (
           [0] => Archie 
           [1] => 42265
           [2] => Nevada
        )
    [1] => Array
        (
           [0] => Gerry
           [1] => 36217
           [2] => Gotham
        )
)

That is the array result after reading data from Excel file.

You can see the result above, the data in array [0][1] and [1][1] doesn't consist date value like the date data from Excel file.

Anyone have an idea how to solve this? or anyone knows why I got this?

Edtited

Code in Controller

This is the code for reading the Excel file

  $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();
        $arrayData = [];
        for ($row = 1; $row <= $highestRow; ++$row) {
            $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
            if (!is_null($rowData[0][0])) {
                $arrayData[] = array_map(function($values) {
                    $tempArrayKey = [];
                    foreach ($values as $key => $value) {
                        $newKey = $key + 1;
                        $tempArrayKey[] = $newKey . '_' . $value;
                    }
                    return $tempArrayKey;
                }, $rowData);

Any help would be appreciated. Thanks

  • 写回答

2条回答 默认 最新

  • doushaqing7080 2016-12-29 09:40
    关注

    you can get date field like this:
    $sheet->setCellValueByColumnAndRow(0, 1, "2014-10-16");
    Sets a string value in the cell, not a date. Just because you interpret that as a date, doesn't mean that computer programs automagically interpret it as a date.
    Or
    You can use this in your updated code:
    $arr[$i] = date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($birthdate));

    评论

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)