Moorcam Posted July 29, 2017 Share Posted July 29, 2017 Howdy, I am using the following code to get the total hours and minutes from multiple input fields. It works fine. However, the final result is displaying the time as: 24.5:30 Where it should be displaying as: 24:30 Here is the code: $day1hours = $mondiff; $day2hours = $tudiff; $day3hours = $weddiff; $day1 = explode(":", $day1hours); $day2 = explode(":", $day2hours); $day3 = explode(":", $day3hours); $totalmins = 0; $totalmins += $day1[0] * 60; $totalmins += $day1[1]; $totalmins += $day2[0] * 60; $totalmins += $day2[1]; $totalmins += $day3[0] * 60; $totalmins += $day3[1]; $hours = $totalmins / 60; $minutes = $totalmins % 60; $totalhours = "$hours:$minutes"; If anyone can spot something that is causing the .5 please show me. I am about to be driven to drinking all the Guinness available in Victoria. Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/ Share on other sites More sharing options...
Solution Jacques1 Posted July 29, 2017 Solution Share Posted July 29, 2017 Hint: 0.5 hours are 30 minutes. What you want is an integer division. Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549039 Share on other sites More sharing options...
HashSoftwares Posted August 1, 2017 Share Posted August 1, 2017 You need to check out the format of the date again. As I have check the code but I didn't get anything wrong. Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549119 Share on other sites More sharing options...
Jacques1 Posted August 1, 2017 Share Posted August 1, 2017 Then you've obviously tested it with full hours. https://3v4l.org/hPa2n Maybe we need a new tutorial: How the clock works. Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549121 Share on other sites More sharing options...
taquitosensei Posted August 2, 2017 Share Posted August 2, 2017 A little less subtly. You've got your answer 24.5 hours which is the same as 24 hours and 30 minutes. Then you tack on :30 for your minutes "$hours:$minutes" 24.5:30 Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549184 Share on other sites More sharing options...
Jacques1 Posted August 2, 2017 Share Posted August 2, 2017 Guys. The problem has been solved last week. You can stop explaining how time works. The OP got it. I know that reading is tough, but when you open a thread, try to at least check the current state. Repeating what everybody else has already said just isn't very helpful. Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549185 Share on other sites More sharing options...
Moorcam Posted August 4, 2017 Author Share Posted August 4, 2017 Hi guys, thanks for the input. Been on tour all week so only had a chance to look now. Yes, I get the 0.5 hours = 30 min. But I only want the hours,minutes to display as 24:30 and not 24.5:30 Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549260 Share on other sites More sharing options...
Jacques1 Posted August 4, 2017 Share Posted August 4, 2017 No shit. How about you read the friggin' replies? Let me try again: You need to do an integer division. Integer division. That means cutting off the fractional part. Like you did in elementary school. 30/60 is 0.5. But intdiv(30, 60) is 0. Because 30 = 0 × 60 + 30. Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549262 Share on other sites More sharing options...
Moorcam Posted August 4, 2017 Author Share Posted August 4, 2017 (edited) No shit. How about you read the friggin' replies? Let me try again: You need to do an integer division. Integer division. That means cutting off the fractional part. Like you did in elementary school. 30/60 is 0.5. But intdiv(30, 60) is 0. Because 30 = 0 × 60 + 30. Jacques1, you having a bad day? I like you so I will forgive you. I did not start shouting abuse at you so do not throw it at me. Okay? So, now that it made more sense, it is working. Thank you. Edited August 4, 2017 by DanEthical Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549263 Share on other sites More sharing options...
Jacques1 Posted August 4, 2017 Share Posted August 4, 2017 My answer had two sentences. Two. When you don't even bother to read two sentences and instead repeat the question as if I was an idiot, then, yes, I'll be less patient than I was the first time. If this was a complex problem with a long answer which cannot be processed in one go, I could see how you might miss some information. But not reading two sentences to me is a sign of disrespect. Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549267 Share on other sites More sharing options...
Moorcam Posted August 4, 2017 Author Share Posted August 4, 2017 My answer had two sentences. Two. When you don't even bother to read two sentences and instead repeat the question as if I was an idiot, then, yes, I'll be less patient than I was the first time. If this was a complex problem with a long answer which cannot be processed in one go, I could see how you might miss some information. But not reading two sentences to me is a sign of disrespect. That's a bit harsh there mate. I read both sentences. However, after driving for 22 hours it made less sense than a chicken crossing the road. My apologies if I seemed disrespectful. Your repeat of those two sentences were what one would call disrespectful along with many other answers I see you give people. We come here for a reason mate, that is to get help with something we are stuck with. We don't come here to get abused. If you do not want to help people, don't post. Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549290 Share on other sites More sharing options...
Jacques1 Posted August 4, 2017 Share Posted August 4, 2017 If you do not want to help people, don't post. If you want a pat on the back rather than technical advice, don't come to a programming forum. 1 Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549292 Share on other sites More sharing options...
Moorcam Posted August 5, 2017 Author Share Posted August 5, 2017 If you want a pat on the back rather than technical advice, don't come to a programming forum. All good mate. Let's not fall out anyway. Quote Link to comment https://forums.phpfreaks.com/topic/304444-total-time-not-displaying-correctly/#findComment-1549300 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.