tags:

views:

60

answers:

4

How can I convert a double value to time? For example, if the input is 7.50 then the output would be 7.30.

A: 
  1. Find the integral part of the number: These are your hours
  2. Find the fractional part of the number; multiply it by 60 and round down (floor()). These are your seconds. That's all there is to it.
tdammers
Thanks...............................
arnold
+3  A: 

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
killer_PL
+2  A: 
$input = "7.5";
list($hours, $wrongMinutes) = explode('.', $input);
$minutes = ($wrongMinutes < 10 ? $wrongMinutes * 10 : $wrongMinutes) * 0.6;
echo $hours . ':' . $minutes;
nikic
Thanks.........................
arnold
+1  A: 

Use this code:

<?php
$input = 7.50;
$hours = intval($input);
$realPart = $input - $hours;
$minutes = intval( $realPart * 60);
echo $hours.":".$minutes;
?>
netme
Thanks.................
arnold