-->

Phpspreadsheet - Time cell retrieved as float

2020-04-19 07:25发布

问题:

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.