joesoaper Posted April 20, 2017 Share Posted April 20, 2017 I have a number of records with the number of hours worked. I now want to add up all the hours in a given period. When it is larger than 24 hours in total then it goes back to zero and starts again. So for example if someone has worked for 27 hours, it shows 3 hours as the total worked. If a person works 23 and a half hours it shows as 23: 30. How can I show the total number of hours when greater than 24. The code I am using is a bit clumsy but it works until I reach 24 hours. Once again any help or advice is greatly appreciated. $sumoftime = strtotime($count_person) + strtotime($count_person1) - strtotime('00:00:00'); $sumoftime = date('H:i:s', $sumoftime); I then echo $sumoftime Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 20, 2017 Share Posted April 20, 2017 How about a simple use of an arithmetic function? Check out the 'mod' function which is '%'. http://php.net/manual/en/internals2.opcodes.mod.php Quote Link to comment Share on other sites More sharing options...
requinix Posted April 20, 2017 Share Posted April 20, 2017 Did you mean to link to that page? Not the arithmetic operators page? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 20, 2017 Share Posted April 20, 2017 Sure. That's the page covering the mod operator Quote Link to comment Share on other sites More sharing options...
requinix Posted April 20, 2017 Share Posted April 20, 2017 It's really more about the opcode, but yeah, it does also show the PHP code to use it... Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 20, 2017 Share Posted April 20, 2017 You know - you're right. I searched on 'mod' and got that page and saw the use of the % operator and copied it. Nice backup! Quote Link to comment Share on other sites More sharing options...
joesoaper Posted April 21, 2017 Author Share Posted April 21, 2017 (edited) After several hours of failure I have sort of found a solution using another method, but might still need advice later today as it involves another method. Thank you for the advice it is very much appreciated. I will open a new topic if I have not got it right by this evening. Edited April 21, 2017 by joesoaper Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 21, 2017 Share Posted April 21, 2017 I offer this code. See if it does what you need. From your description it should be right. for ($i=5; $i< 40; $i+=4) { $result = $i % 24; echo "$i % 24 = ".$result."<br>"; } Quote Link to comment Share on other sites More sharing options...
requinix Posted April 21, 2017 Share Posted April 21, 2017 ...hold on a second here. ginerjm, I think you misread the question: it's about not reducing the hours to $sumoftime = strtotime($count_person) + strtotime($count_person1) - strtotime('00:00:00'); $sumoftime = date('H:i:s', $sumoftime);joesoaper, you're treating the result like it was a time of day. That can never show anything more than 24 hours. DateInterval would be the most appropriate thing except it doesn't really have a way of doing this. Could do something awkward with date math, but I would probably just do it all manually. What are $count_person and $count_person1? Times? Like 08:00:00 for 8 hours? list($h, $m, $s) = explode(":", "12:34:56"); list($h2, $m2, $s2) = explode(":", "23:45:01"); $s += $s2; $m += floor($s / 60) + $m2; $s %= 60; $h += floor($m / 60) + $h2; $m %= 60; printf("%02d:%02d:%02d", $h, $m, $s); // 36:19:57 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 21, 2017 Share Posted April 21, 2017 Hmmm... From post #1: When it is larger than 24 hours in total then it goes back to zero and starts again. So for example if someone has worked for 27 hours, it shows 3 hours as the total worked. That says to me that he wants the modulus of 24 for his total hours. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 21, 2017 Share Posted April 21, 2017 I read it as When it is larger than 24 hours in total then it goes back to zero and starts again. So for example if someone has worked for 27 hours, it shows 3 hours as the total worked. If a person works 23 and a half hours it shows as 23: 30.that's what it does now, The code I am using is a bit clumsy but it works until I reach 24 hours.that's the problem with what it's doing now, How can I show the total number of hours when greater than 24.and that's what it needs to do. Besides, the code from that post will do the whole modulo thing already by virtue of using a date/time-based representation for the total time. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 21, 2017 Share Posted April 21, 2017 Upon further review... Not clearly stated enough for this old Polack. I didn't realize that he "didn't like" what he had. And his code made no sense at all. Why subtract a 0 value from his addition sequence? 1 Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 21, 2017 Share Posted April 21, 2017 (edited) After several hours of failure I have sort of found a solution using another method, but might still need advice later today as it involves another method. Thank you for the advice it is very much appreciated. I will open a new topic if I have not got it right by this evening. You should show your solution for anyone else that may find this thread trying to solve the same problem. I think the root cause is that you are storing a "time" value. Then you convert those to a timestamp and try to add them. The conversion creates a complete date/time value. So, if you have hours of 14:00 and 16:00, you want a total of 30:00. But, that is not a valid "time". I think you need to convert the times to decimal values, add them, then convert back to "time" format. Or you could just store/use all the values in decimal format and only convert to a "time" format upon display. Edited April 21, 2017 by Psycho Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 21, 2017 Share Posted April 21, 2017 Here's a quick modification of a couple functions I have. maybe these will help <?php //Converts a time string ('hh:mm:ss') to an integer for the total seconds function timeToSeconds($time) { list($hours, $minutes, $seconds) = explode(':', $time); $secondsInt = ($hours * 3600) + ($minutes * 60) + $seconds; return $secondsInt; } //Convert an integer of seconds to time format function secondsToTime($secondsInt) { $hours = floor( $secondsInt / 3600 ); $minutes = floor( ($secondsInt/60) % 60 ); $seconds = floor( $secondsInt % 60 ); $time = sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); return $time; } //Usage $time1 = '12:15:35'; $time2 = '08:20:15'; $time3 = '06:15:05'; //Add the total seconds for each time value $totalSeconds = timeToSeconds($time1) + timeToSeconds($time2) + timeToSeconds($time3); //Convert the total seconds back to a time format $timeFormat = secondsToTime($totalSeconds); echo "Total seconds: {$totalSeconds}"; //Output: 96655 echo "Time format: {$timeFormat}"; //Output: 26:50:55 ?> 1 Quote Link to comment Share on other sites More sharing options...
joesoaper Posted April 22, 2017 Author Share Posted April 22, 2017 (edited) So first of all once again thank you for all the help and advice. I have - after hours of trying different options - realised that I had a fundamental error in my thinking. As long as it was coded in time as in clock time, it would always go from 24:00 to 00:00, so the use of strtotime was not helping me at all. After more research i came across SELECT SEC_TO_TIME( SUM( TIME_TO_SEC which has half resolved my problem. Using this I now get the total number of hours worked from the field overtime_hours. However I need help in getting the total from TWO fields at the same time viz: overtime_hours AND overtime_hours1. For some reason I just cannot get the syntax right when attempting to get the total from 2 fields. Here below is my working code for getting the correct result from the one field overtime_hours. As always any suggestions and advice is greatly appreciated. $count_total_hours_worked = $pdo->query("SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `overtime_hours` ) ) )FROM dbase WHERE staff='Sam' ")->fetchColumn(); Edited April 22, 2017 by joesoaper Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 23, 2017 Share Posted April 23, 2017 Try SELECT SEC_TO_TIME( SUM( TIME_TO_SEC(`overtime_hours`) + TIME_TO_SEC(`overtime_hours1`) ) ) FROM dbase WHERE staff='Sam' Quote Link to comment 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.