doutang9037 2016-11-19 15:53 采纳率: 100%
浏览 51
已采纳

从上传的Excel文件中读取日期

I am uploading an Excel file through PHP Excel. The column C has dates in format mm/dd/yyy.

When I echo the date for example '10/3/2016' it reads it as 42646. The format set for the column is short Date.

How can I read it as the normal date instead of 42646.

here is the model

function fi_upload($file_name){
$file = './uploads/'.$file_name;
$this->load->library('excel');
$objPHPExcel = PHPExcel_IOFactory::load($file);
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
foreach ($cell_collection as $cell) {
    $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
    $row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
    $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();

    if ($row == 1) {
        $header[$row][$column] = $data_value;
    } else {
        $arr_data[$row][$column] = $data_value;
    }
}
$data['header'] = $header;
$data['values'] = $arr_data;
$datecell = $objPHPExcel->getActiveSheet()->getCell('P2');
if(PHPExcel_Shared_Date::isDateTime($datecell)) {
     $InvDate = date($format="Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($datecell)); 
}
echo $InvDate;
exit();


foreach ($arr_data as $q){
echo $q['C']; // column of date, date is echoed as 42646
exit(); //here i exit to display the date
}

I have to later insert it in the database as well in the yyyy/mm/dd format (which I can do if it is read properly)

Thanks.

UPDATE:

So apparently it was an Excel file issue, not php. Converting the cell values through TEXT() was the solution as suggested by @Hallur.

UPDATE 2.0: I am trying to do it via PHPEXCEL method but I get the following two issues.

  1. : Object of class PHPExcel_Cell could not be converted to int
  2. Echoes date as 2036-02-07 where as the date in cell is 10/4/2016
  • 写回答

3条回答 默认 最新

  • doufang8965 2016-11-19 16:51
    关注

    42646 is an MS Excel serialized timestamp value, the number of days since 1st January 1900 (or 1st January 1904, depending on whether the spreadsheet is using the Windows or Mac calendar)

    PHPExcel provides a variety of functions to convert values between MS Excel serialized timestamps and Unix timestamps or PHP DateTime objects (or vice versa), all of which can be found in the PHPExcel_Shared_Date class, e.g.

    • ExcelToPHP() to convert an MS Excel serialized timestamp to a unix timestamp
    • ExcelToPHPObject() to convert an MS Excel serialized timestamp to a PHP DateTime object

    Alternatively, using the getFormattedValue() method instead of getValue() will return a formatted date/time string value instead of the serialized tiemstamp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站