Jump to content

[SOLVED] Minutes to hour calculation


vinoindiamca

Recommended Posts

Hi i need to calculate minutes to hours calculation

i got output like 11:79

then i coded like

 

if($timmin>60)

{

$newtimmin=intval(floor($timmin/60));

$newtimhr=intval(floor($timhr+$newtimmin));

}

else

{

$newtimmin=$timmin;

$newtimhr=$timhr;

}

$time_add = $newtimhr.":".$newtimmin;

 

After this coding i got output as 12:1 here 12 hours is correct but minutes to be 19 right

12:19 to be return please help me

 

Link to comment
https://forums.phpfreaks.com/topic/148892-solved-minutes-to-hour-calculation/
Share on other sites

You got 12:1 because floor(79/60) = floor(1.3166) = 1.

 

The modulus operator (%) is the key to this one.  It returns the remainder of division.

 

//Time is 11:79
//$tM is minute portion of time
//$tH  is hour portion of time
//$ntM is minute portion of time after operations
//$ntH is hour portion of time after operations
//$time_add is the string containing the readable time

$ntM = $tM % 60; //in this case returns 19;
$ntH = $tH + ($tM-$ntM)/60; //in this case 11+(79-19)/60 = 11+60/60 = 12;
$time_add = $ntH.":".$ntM;

 

Note that the above code acts correctly for all possible values, including times that are already "proper" (i.e. 11:36) so you don't need the if-else statement.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.