phpexcelphpexcel

Read Date Format in PHP EXCEL


I have read excel file using PHP excel .

But that excel file contain some date (time format) PHP excel return wrong values for that case

My code is in below

enter code hereinclude 'Classes/PHPExcel/IOFactory.php';

$inputFileName = 'index.xlsx';

//  Read your Excel workbook
try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
    die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
        . '": ' . $e->getMessage());
}

//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();

//  Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++) {
    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
        NULL, TRUE, FALSE);

    foreach ($rowData[0] as $k => $v)
        echo "Row: " . $row . "- Col: " . ($k + 1) . " = " . $v . "<br />";
}

any one can help for this.. Have some specific function for this case in PHPExcel..

My input excel file is in below

2013-12-12, 2013-12-13

My output is in below

41621, 41631

Have some method to covert date format output data?


Solution

  • When you read xls file with PHPExcel_Reader_Excel5 lib, the data in file is 39984, but Excel formats it using a date formatting mask as '2009-06-20'?

    Excel holds dates as a count of the number of days since 1st January 1900 (on Windows 1900 calendar). PHPExcel stores its dates in the same way, but doesn't automatically format them for you.

    You can format them yourself using PHPExcel_Style_NumberFormat::toFormattedString(39984,PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY)
    or any of the other format masks in
    PHPExcel_Style_NumberFormat
    , or convert it to a PHP date using
    PHPExcel_Shared_Date::ExcelToPHP(39984) and then use PHP's date()
    function to format it as you wish

    Example:

    $val = date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($cell->getValue()));