简体   繁体   中英

Get datetime with phpExcel

My column has the format of cell: dd / mm / yyyy hh: mm, when reading these worksheets, I need to get this value only the date and save in mysql.

By default, I'm reading the date, and giving exploits to save it, but the date comes in a weird format.

Column date in cell: 13/11/2017 00:01

My code:

    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objReader->setReadDataOnly(true);
    $objPHPExcel = $objReader->load($inputFileName);
    //Get worksheet and built array with first row as header
    $objWorksheet = $objPHPExcel->getActiveSheet();
    //excel with first row header, use header as key
    if($header){
        $highestRow = $objWorksheet->getHighestRow();
        $highestColumn = $objWorksheet->getHighestColumn();
        $headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null, true, true, true);
        $headingsArray = $headingsArray[1];
        $r = -1;
        $namedDataArray = array();
        for ($row = 2; $row <= $highestRow; ++$row) {
            $dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
            if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
                ++$r;
                foreach($headingsArray as $columnKey => $columnHeading) {
                    $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
                }
            }
        }
    }
    else{
        //excel sheet with no header
        $namedDataArray = $objWorksheet->toArray(null,true,true,true);
    } 

When reading the value directly is to give an echo: 43052.000717593

My code to just get the date in the format I need and save

    $data_mailing_file = explode(" ", $value['field_date']);

    $dataP = explode('/', $data_mailing_file[0]);
    $data_save = $dataP[2].'-'.$dataP[1].'-'.$dataP[0];

The 43052.000717593 is an MS Excel serialized timestamp value, a count of the number of days since 1st January 1900 (or 1st January 1904 if the spreadsheet is using the Mac calendar).

MS Excel formats that value using the format mask, which you say is dd/mm/yyyy hh:mm and display that value as 13/11/2017 00:01 , which is exactly what I'd expect from that format mask.

If you want to format the date differently; then you could change the format mask before calling rangeToArray() . An Excel format mask of yyyy-mm-dd will give you the date only in an appropriate format.

Alternatively get that serialized timestamp value, and then use the ExcelToPHP() or ExcelToPHPObject() methods of PHPExcel_Shared_Date to give a unix timestamp or a PHP DateTime object respectively, that you can then format however you want using PHP's date() function or the DateTime object's format() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM