Reading date format from excel to PHP

Topics: Developer Forum, User Forum
Nov 20, 2012 at 6:11 PM

Hi there,

I have a problem when I tried to read an excel file to PHP. I used the PHPExcel 1.7.7 version right now.

Here is a sample from the excel 2007 file (Column A for name, and column B for date)

Jim 9/17/2010
Gordon 6/4/1979
Bill 3/24/1987
Steve 5/24/1991
Robin 8/8/1964

I used this code to read the name and birthday:

include 'PHPExcel/IOFactory.php';
$inputFileName = 'example.xls';

echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format
'
; $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); foreach ($sheetData as $row) { echo $row['A']."
"
; echo date_format(date_create_from_format('m-d-y', $row['B']), 'Y-m-d')."
"; }

In the end it will print something like this

Jim 2010-09-17
Gordon 1979-06-04
Bill 1987-03-24
Steve 1991-05-24
Robin 2064-08-08

How do i change for the last part so the year would be 1964?

Coordinator
Nov 20, 2012 at 8:17 PM

I think you might need:

echo date_format(date_create_from_format('m-d-Y', $row['B']), 'Y-m-d');

Y rather than y in the date_create_from_format() method