Reputation: 13
I am trying to import an excel file into my database in PHP. The file contains a column which has time values in the format H:MM. When I try to read this column, I get garbage values like 0.416666666667.
My code goes here :
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1252');
$data->setUTFEncoder('mb');
$data->read($filePath);
echo $data->sheets[0]['cells'][$i][$j];
Am I missing something? The value in the column is 10:00 but all I get is 0.416666666667.
Please help.
Upvotes: 1
Views: 1506
Reputation: 1007
I am seeing this rather late, but putting down this answer for helping someone out in future.
You can format the time in a single line code using date
function like below:
$testval = $data->sheets[0]['cells'][$i][$j];
//assuming for the sake of example here, time format is HH:MM:SS am/pm
echo date('g:i:s a',mktime(0,0,0)+86400*$testval)
You can get complete list of format types from this page
Upvotes: 0
Reputation: 212402
The value in the MS Excel cell is a serialized timestamp, it's formatted as a human readable date/time by the format mask, which SER isn't particularly good at handling
$time = 0.416666666667 * 86400;
$hours = round($time / 3600);
$minutes = round($time / 60) - ($hours * 60);
$seconds = round($time) - ($hours * 3600) - ($minutes * 60);
echo sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
Upvotes: 2