Reputation: 19
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
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.
Upvotes: 3