Sajith Shyamal
Sajith Shyamal

Reputation: 19

PHP return floating point number as time?

I want to import excel sheet values to MySQL table through PHP. There is column as time , value is 8:30 in excel. When I get that value to PHP it's represented as 0.35416666666667, I'm using phpexcel library .

$object = PHPExcel_IOFactory::load($_FILES["excel_file"]["tmp_name"]);
            foreach ($object->getWorksheetIterator() as $worksheet){
                $highestrow = $worksheet->getHighestRow();
                for ($row=2; $row<=2; $row++)
                {
                   echo $name = $worksheet->getCellByColumnAndRow(0,$row)->getValue();
}
}

I want that value to also be represented in PHP as 08:30:00

Upvotes: 0

Views: 271

Answers (1)

Nick
Nick

Reputation: 147166

Excel represents times as fractions of a day, so to convert an Excel timevalue to a PHP time string you need to convert it to seconds, then convert that to a date/time variable and output the time part of it. e.g.

$timevalue = 0.35416666666667;
echo gmdate('H:i', $timevalue * 24 * 3600);

Output:

08:30

Note we use gmdate rather than date to avoid issues with summer time adjusting the hour by 1.

Demo on 3v4l.org

Upvotes: 3

Related Questions