Phpspreadsheet - Time cell retrieved as float

前提是你 提交于 2020-01-30 09:50:29

问题


I have an excel file which has a time input.

  Mar 01, 2018  | Thursday  | 8:00 AM | 5:00 PM
  Mar 02, 2018  | Friday    | 8:00 AM | 5:00 PM

But when my code tries to read those cells, the output becomes a float (for example 8:00 AM becomes 0.33333333333333). This is my code

$date = $sheet->getCell("A".$row)->getValue();
$time_in = $sheet->getCell("C".$row)->getValue();
$time_out = $sheet->getCell("D".$row)->getValue();

echo "date: ".$date.          //Mar 01, 2018
     " time_in: ".$time_in.   //becomes 0.333333333333333
     " time_out: ".$time_out; //becomes 0.708333333333333

How can I make the output as is without the phpspreadsheet changing the value? I have tried looking at the phpspreadsheet documentation but I haven't found a solution.


回答1:


Thankfully I have found the answer just now.

$in = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($time_in);
echo gmdate("g:i a", $in);

Hopefully, this could be useful for others.



来源:https://stackoverflow.com/questions/49142511/phpspreadsheet-time-cell-retrieved-as-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!