4

Hi not sure if this is the right forum for this but does anyone know the formula for converting decimal time in to hours and minutes ?

IE 1.4 = 1hr 24mins

thanks for any help and sorry if its the wrong forum

Mick
  • 2,840
  • 10
  • 45
  • 61
  • 1
    Not sure what you intend on using this for, but if you're using this to describe how long ago something happened from the current point in time, take a look at: http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time/501415#501415 – John Rasch Apr 15 '10 at 18:58

4 Answers4

15
$decTime = 1.4;
$hour = floor($decTime);
$min = round(60*($decTime-$hour));
James B
  • 8,183
  • 4
  • 33
  • 40
  • Note the round on the minutes. I had it as a floor and some of the time display wasn't showing correctly by a minute. So annoying! Thanks guys! – cbloss793 Oct 26 '16 at 16:14
4

Simply take the decimal portion of the hours and multiply by 60 for the number of minutes.

In your example .4 (the decimal portion of 1.4) * 60 = 24 minutes

So if you need to do it in code, subtract the floor of the original number from the original number to get the decimal portion.

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
2

Hours: floor( decimal )
Minutes: round( ( decimal * 60 ) mod 60 )

James is right about the rounding. I forgot to account for that. Adjusted.

Community
  • 1
  • 1
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
1

Floor the number to get how many hours. Number - floored value [the decimal] * 60 = minutes [round it after]

Warty
  • 7,237
  • 1
  • 31
  • 49