vinoindiamca Posted March 11, 2009 Share Posted March 11, 2009 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 More sharing options...
T-Bird Posted March 11, 2009 Share Posted March 11, 2009 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. Link to comment https://forums.phpfreaks.com/topic/148892-solved-minutes-to-hour-calculation/#findComment-781831 Share on other sites More sharing options...
vinoindiamca Posted March 11, 2009 Author Share Posted March 11, 2009 Hello Sir, Thank you ver much its working now, Pleasure to share logic with you. Link to comment https://forums.phpfreaks.com/topic/148892-solved-minutes-to-hour-calculation/#findComment-781848 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.