How to read a time value?

Topics: Developer Forum, User Forum
Feb 1, 2010 at 11:09 PM

Hi I have just started with PHPExcel and am trying to read a time value from an xlsx but the data I'm extracted isn't correct.  In Excel I have set the cell format as "1:30:00 PM" from the Time tab of the Format Cells option and entered a date of 8:30:00 AM.  I'm using the following code to extract the cell data -

$start = ($objPHPExcel->getActiveSheet()->getCell('D7'));
echo 'start '.$start->getValue();

What actually gets echo'd to the screen is -


Do I have to perform a formatting operation on this return value?  Or is how I'm extracting the data wrong?  I greatly appreciate any help that can be given.

Feb 2, 2010 at 4:21 AM

Do like this for now:


$value = $objPHPExcel->getActiveSheet()->getCell('D7')->getValue());
var_dump($value); // float(0.3541666666666667)

$formatCode = $objPHPExcel->getActiveSheet()->getStyle('D7')->getNumberFormat()->getFormatCode();
var_dump($formatCode); // string(13) "h:mm:ss AM/PM"

$formattedString = PHPExcel_Style_NumberFormat::toFormattedString($value, $formatCode);
var_dump($formattedString); // string(10) "8:30:00 AM"

In the future there may be an easier way to accomplish this.

Feb 2, 2010 at 7:00 PM

Koyama, thank you for your help.  Your solution solves the problem perfectly.