3

I have used a few forms to establish a duration of time that a user has worked as well as get the amount of overtime they have worked. Problem is that I want to know how many hours can be billed in overtime and how many is normal time. Now its rather obvious, subtract overtime from total time,however I used the following script to obtain the time amounts:

$logtime = new CGenRs("SELECT time_id,
  profile_id ,
  user_number ,
  start_time ,
  end_time,
  description,
  exported,ovt_start_time,ovt_end_time, EXTRACT(hour FROM(end_time - start_time)) as diff,EXTRACT(minute FROM(end_time - start_time))as difference,EXTRACT(hour FROM(ovt_end_time-ovt_start_time)) as ovt_diff,EXTRACT(minute FROM(ovt_end_time-ovt_start_time)) as ovt_difference from adapt_profile_time where exported = 'f' order by user_number", $cao);
$logtime->first();

The Extract feature works well to show the times but when I start to subtract its where it gets a bit messy.

$tot_mins = 0;
            $ovt_mins = 0;
        }
        $tot_mins = ($tot_mins + $logtime->valueof('diff') * 60) + $logtime->valueof('difference');
        $ovt_mins = ($ovt_mins + $logtime->valueof('ovt_diff') * 60) + $logtime->valueof('ovt_difference');
    $total_test= floor(($tot_mins / 60)-($ovt_mins / 60))." hours ".(($tot_mins % 60)-($ovt_mins % 60))." minutes ";

When using the echo $total_test it does the calculation but if the user has worked 7 hours 0 minutes which consists out of 3 hours 40 minutes overtime the result of the above calculation returns 3 hours -40 minutes. Which is wrong. So where am I going wrong here?

I believe the problem lies with the EXTRACT hours and minutes not working well with the "-" "+" operators. I added a total colum in my table that adds the total time with the overtime (It should be subtracted but I did it to test) I used the following code:

<td><?php echo ($logtime->valueof('diff')   +  $logtime->valueof('ovt_diff')) . " " . "hours" . " ".(($logtime->valueof('difference') + $logtime->valueof('ovt_difference')))." "."minutes"  ?></td>

The result was interesting. If user worked 3 hours 50 minutes of which it all was overtime, the result returned was 6 hours 100 minutes. So the addition is working, its just the values aren't recognized in a time format

3
  • my first guess is that you use the date functions not properly. postgresql should be able to directly subtract two dates from one another. the result should be something like an interval. extract on the other hand extracts a part from a date, like the hour part. so my guess is that part of your problem is based on this. maybe this related question helps you stackoverflow.com/questions/1964544/…
    – Joshua
    Oct 21, 2015 at 7:34
  • I agree with Joshua. You should be able to do this with your database. But incase you want to do it in php, you really need to look at the DateTime object and methods. Calculating dates and such yourself is quite anoying. Check out php.net/manual/en/datetime.diff.php for example. Oct 21, 2015 at 7:36
  • @Joshua Thanks guys, reading through them now, Will post a solution shortly Oct 21, 2015 at 7:39

2 Answers 2

1

As I stated in the comments, it is wise to use the DateTime classes for any date/time actions you want to do. Including getting the difference between times.

Check out this example:

// Create two new DateTime-objects...
$Date1 = new DateTime('2015-10-21T8:00:00');
$Date2 = new DateTime('2015-10-21T18:30:00');

// The diff-methods returns a new DateInterval-object...
$Diff = $Date2->diff($Date1);

// Call the format method on the DateInterval-object
echo $Diff->format('%h:%i');

The output from the code above should be "10:30"

From here on you can simply get the difference between the times and check if it is more then 8 hours. If it is, you can get your amount of overtime.

4
  • What I'm thinking is that I should have my SQL do the calculations. Basically having it return a time-stamp difference and then just editing the formatting in php.Will post result as soon as I get it working Oct 21, 2015 at 7:54
  • That is the best option imho. I gave my answer in PHP because that is what you asked, but my general rule of thumb is to always use your database's functions whenever possible. Oct 21, 2015 at 7:58
  • Yes, Thanks for the answer. It made me research PHP's DateTime classes and I only now realize how powerful it really is Oct 21, 2015 at 8:00
  • Good to hear :) I've seen way too many struggle with Date/Time calculations, while there are classes that make it so easy. Good luck with your case! :) Oct 21, 2015 at 8:10
0

I eventually got it working. I changed my query to the following:

$logtime = new CGenRs("SELECT time_id,
  profile_id ,
  user_number ,
  start_time ,
  end_time,
  description,
  exported,ovt_start_time,ovt_end_time, EXTRACT(hour FROM(end_time - start_time)) as diff,EXTRACT(minute FROM(end_time - start_time))as difference ,EXTRACT(hour FROM(ovt_end_time-ovt_start_time)) as ovt_diff,EXTRACT(minute FROM(ovt_end_time-ovt_start_time)) as ovt_difference,EXTRACT(EPOCH FROM (end_time - start_time)) as super,EXTRACT(EPOCH FROM (ovt_end_time - ovt_start_time)) as spar FROM adapt_profile_time where exported = 'f' order by user_number", $cao);
$logtime->first();

As you can see I added the EXTRACT Epoch to the query for both the over time and the total work time.

I then added the next piece of code:

<td><?php $epoch_1 = $logtime->valueof('super');
                $epoch_2 = $logtime->valueof('spar');
                $diff_seconds = $epoch_1 - $epoch_2;
                $diff_weeks = floor($diff_seconds / 604800);
                $diff_seconds -= $diff_weeks * 604800;
                $diff_days = floor($diff_seconds / 86400);
                $diff_seconds -= $diff_days * 86400;
                $diff_hours = floor($diff_seconds / 3600);
                $diff_seconds -= $diff_hours * 3600;
                $diff_minutes = floor($diff_seconds / 60);
                $diff_seconds -= $diff_minutes * 60;
                echo $diff_hours." "."hours ".$diff_minutes." minutes" ?></td>
                <td>

And so I got the correct values to display in the table. That being done I changed the code in my question to the following:

$tot_mins = 0;
    $ovt_mins = 0;
    $total_mens = ($logtime->valueof('super') /60 ) + ($logtime->valueof('spar')/60);


    while (!$logtime->eof()) {
        while (!$logtime->eof()) {


            if ($curr_userno != $logtime->valueof('user_number')) {
                $total_time = floor($tot_mins / 60) . " hours " . ($tot_mins % 60) . " minutes";
                $total_ovt = floor($ovt_mins / 60) . " hours " . ($ovt_mins % 60) . " minutes";
                $total_test = floor($total_mens/60)." hours ".($total_mens%60). " minutes";

And now its working.The values are adding up perfectly and carrying over when necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.