Jump to content

Show number of hours when greater than 24 hours


joesoaper

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

...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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
 
?>
Link to comment
Share on other sites

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();
Link to comment
Share on other sites

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.