Read excel xlsx file using simplexlsx in php

后端 未结 1 1406
失恋的感觉
失恋的感觉 2021-02-14 14:04

I am using simplexlsx.class.php to read xlsx file type. It\'s giving problems when the file contain date field in the excel file.

Sample output:

相关标签:
1条回答
  • 2021-02-14 14:43

    Those are the correct dates, just in Excel's internal format: number of days since Jan 1st 1900 (allowing for 1900 being a leap year). Clearly something in the simplexlsx class is converting the xlsx date value to the Excel internal format.

    I've not come across simplexlsx before (which surprises me as I thought I knew all the Excel file reader/writer libraries for PHP)... but somewhere in the code there must be a method to handle that conversion, so I'd imagine that there would also be a method for the reverse (converting Excel timestamp to PHP)

    EDIT

    The method you want is in the code:

    function unixstamp( $excelDateTime ) {
        $d = floor( $excelDateTime ); // seconds since 1900
        $t = $excelDateTime - $d;
        return ($d > 0) ? ( $d - 25569 ) * 86400 + $t * 86400 : $t * 86400;
    }
    

    I make no guarantees that it's accurate

    EDIT FURTHER

    function unixstamp( $excelDateTime ) {
        $d = floor( $excelDateTime ); // seconds since 1900
        $t = $excelDateTime - $d;
        return ($d > 0) ? ( $d - 25569 ) * 86400 + $t * 86400 : $t * 86400;
    }
    
    
    $dateVal = 40941;
    $unixDateVal = unixstamp($dateVal);
    var_dump($unixDateVal);
    echo date('d-M-Y',$unixDateVal);
    

    gives

    float 1328140800
    

    which looks remarkably like a unix timestamp value in the correct range for this year, and sure enough:

    02-Feb-2012
    

    So looks like it works to me

    0 讨论(0)
提交回复
热议问题