How to read date?

Topics: Developer Forum, User Forum
Feb 3, 2010 at 9:39 PM

Hi I'm having trouble finding how to read a value from a cell which is formatted as a date.  Can anyone help me out and point me in the direction of some sample code please?

Coordinator
Feb 3, 2010 at 10:29 PM
$value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue());
$formatCode = $objPHPExcel->getActiveSheet()->getStyle($cell)->getNumberFormat()->getFormatCode();
$formattedDateString = PHPExcel_Style_NumberFormat::toFormattedString($value, $formatCode);
var_dump($formattedDateString);
Dec 7, 2010 at 3:07 PM

Mark,

I'm sure this method will work correctly, however, I have PHP version 5.1 on a CentOS 5.5 install. When I use your method above, I get "PHP Fatal error:  Call to undefined function date_create() in PHPExcel/Shared/Date.php on line 124". I do understand that PHPExcel's requires PHP 5.2, but is there a recommended alternative method than using the date_create function on that line? As you know, the DateTime class doesn't exist with PHP 5.1.

Thanks in advance.

Coordinator
Dec 7, 2010 at 10:35 PM

@lieutdan13

It is possible to build PHP 5.1.x with the DateTime class, although it's only available within the PHP core since 5.2.0.

Note: Experimental DateTime support in PHP 5.1.x

Although the DateTime class (and related functions) are enabled by default since PHP 5.2.0, it is possible to add experimental support into PHP 5.1.x by using the following flag before configure/compile: CFLAGS=-DEXPERIMENTAL_DATE_SUPPORT=1

 

Apr 18, 2011 at 5:47 PM

This sample code was most helpful; thanks. :)