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,770
  • 9
  • 43
  • 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
  • 7,862
  • 4
  • 31
  • 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
  • 53,439
  • 13
  • 112
  • 113
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
  • 21,869
  • 8
  • 72
  • 103
1

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

Warty
  • 7,010
  • 1
  • 28
  • 47