4

How i cant use the date from an excel. I use a for loop to output the file

echo $sheet -> getCellByColumnAndRow($j,$i)->getValue();

But the output looks like that:

 Date  |  Name
---------------
425831 |  Peter
425831 |  Nils

How i get something like that 2016-08.01?

nicoschuck
  • 201
  • 3
  • 13
  • `getFormattedValue()` might be better than `getValue()`, as (unles you've specified loadDataOnly as true when loading the file, then it will convert that MS Excel serialized timestamp to a formatted string reflecting the date – Mark Baker Sep 29 '16 at 16:05

1 Answers1

1

What you are getting is seconds from 1970. Try something similar to this, as mentioned in this answer:

$cell = $excel->getActiveSheet()->getCell('B' . $i);
$InvDate= $cell->getValue();
if(PHPExcel_Shared_Date::isDateTime($cell)) {
    $InvDate = date($format, PHPExcel_Shared_Date::ExcelToPHP($InvDate));
}

You can however also just convert it in PHP with this:

date('Y-m-d',PHPExcel_Shared_Date::ExcelToPHP($sheet -> getCellByColumnAndRow(1,12)->getValue()));
Community
  • 1
  • 1
Philip
  • 95
  • 1
  • 9
  • i tried echo date('Y-m-d',$sheet -> getCellByColumnAndRow(1,12)->getValue()); i got 1970-01-01. but why – nicoschuck Sep 29 '16 at 16:00
  • 2
    `seconds from 1870`.... days from 1-Jan-1900 (or 1904 on Mac) in fact; that's an MS Excel Serialized timestamp, not a Unix timestamp – Mark Baker Sep 29 '16 at 16:01
  • 1
    Didnt saw the link echo date('Y-m-d',PHPExcel_Shared_Date::ExcelToPHP($sheet -> getCellByColumnAndRow(1,12)->getValue())); works. THANK YOU FOR YOUR HELP – nicoschuck Sep 29 '16 at 16:04
  • Thanks for the clarification @MarkBaker I wasn't even aware MS used their own standard. – Philip Sep 29 '16 at 16:37