Friday, 15 July 2011

php - time conversion to float -



php - time conversion to float -

i wrote code convert float value of time , convert hours , minutes. if input 2.5 homecoming 2:30 (2 hours , 30 minutes).

$input = '2:5'; $num_hours=str_replace(":",".",$input,$num_hours) ; $hours = floor($num_hours); $mins = round(($num_hours - $hours) * 60); echo $hour_one = $hours.'.'.$mins;

but want reverse it: if input 2:30 should homecoming 2.5.

you can utilize modulo % operator first operation:

$value = 2.5; echo floor($value) . ':' . (($value * 60) % 60);

output:

class="lang-none prettyprint-override">2:30

to convert them can use:

$value = "2:30"; $parts = explode(':', $value); echo $parts[0] + floor(($parts[1]/60)*100) / 100 . php_eol; class="lang-none prettyprint-override">2.5

php

No comments:

Post a Comment