How can I convert a double value to time? For example, if the input is 7.50
then the output would be 7.30
.
$input = "7.5";
list($hours, $wrongMinutes) = explode('.', $input);
$minutes = ($wrongMinutes < 10 ? $wrongMinutes * 10 : $wrongMinutes) * 0.6;
echo $hours . ':' . $minutes;
Get only a nondecimal rest by
$val = $input - floor( $input );
Then convert values from 0-99 to 0-60 by (exactly it is 0.99... to 0.60):
$val = $val*0.6;
At the end add the computed precision value to the decimal part of the input:
$output = floor( $input ) + $val
Use this code:
<?php
$input = 7.50;
$hours = intval($input);
$realPart = $input - $hours;
$minutes = intval( $realPart * 60);
echo $hours.":".$minutes;
?>