Reputation: 72
I am doing an excel import function in my PHP project. I have a time field in my excel (like 16:30) but when I import this excel into a database, it converts to 0.6875 (16.5/24)
How can I convert 0.6875 to 16:30?
Note: this excel file cannot be modified because it was generated by a machine.
Upvotes: 1
Views: 2685
Reputation: 17417
I stand by what I said in the comment about this fixing the wrong problem, but for the record this can be solved in two lines if you definitely need it:
$time = 0.6875 * 86400;
echo date('H:i', $time);
16:30
Upvotes: 3
Reputation: 1429
If you really need to do this (and I'm assuming it is because of some odd requirement with computer generated files with odd output), then you can simply undo the operation you have done with some pretty printing to help.
$the_value = .6875;
$total = $the_value * 24; //multiply by the 24 hours
$hours = floor($total); //Gets the natural number part
$minute_fraction = $total - $hours; //Now has only the decimal part
$minutes = $minute_fraction * 60; //Get the number of minutes
$display = $hours . ":" . $minutes;
If you need seconds, then you can do to minutes what you did to hours.
$minutes_whole = floor( $minutes );
$seconds_fraction = $minutes - $minutes_whole;
$seconds = $seconds_fraction * 60;
Same for milliseconds, etc. ad nauseum.
Upvotes: 2