0

I'm having a bit of an issue getting the hours worked to display the correct hours worked.

Here is the code that I am using:

<?php while($row = $approvedTimes->fetch()){ ?>
    <tr>
      <td><?php echo $row['userFirst'] . " ". $row['userLast'];  ?></td>
      <td><?php echo $row['companyName'];  ?></td>
      <td><?php echo date('Y-M-d',strtotime($row['timeDate']));  ?></td>
      <td><?php echo date("h:i a",strtotime($row['timeStart'])); $start = $row['timeStart'];  ?></td>
      <td><?php echo date("h:i a",strtotime($row['timeEnd'])); $end = $row['timeEnd'] ;  ?></td>
      <td><?php echo $row['timeBreak'] . " min."; $break = $row['timeBreak']; ?></td>
      <td><?php $hours = strtotime($end) - strtotime($start) -($break*60) ; echo  number_format($hours/3600,2, '.', '') . " hours"; ?></td>
    </tr>
    <?php } ?>

If you notice the second record is outputting -17.50 hours but the hours worked should be 6.5 hours. I believe that the problem is that it is seeing both times in seconds but I'm unsure of how to be able to change it to display the correct hours worked based on the start and end time.

This is the table I output:

Time Table with error

Community
  • 1
  • 1
WorkHardWork
  • 92
  • 10
  • you must supply a complete date / time for strtotime(). If you omit the date, it assumes "today". 5:30pm is AFTER 12:30 am, so I'm assuming he ended the next day – Honk der Hase Jul 25 '16 at 15:15

3 Answers3

0

No short answer possible so, you have to use something like this:

$hours = strtotime($row['timeEndDate']." ".$row['timeEnd']) 
- strtotime($row['timeStartDate']." ".$row['timeStart']) 
-($break*60);

But you have to collect the endtime date too!

As Lars Stegelitz commented: Your periods are maybe overnight!

So before do any changes think about collecting the timeStart and timeEnd as full datetime instead of time only.

If you want to do this with the current table, you have to make addional checks like: Is the start time greater as the end time and then react on that (strtotime() with plus on day)

How do I find the hour difference between two dates in PHP?

Calculate number of hours between 2 dates in PHP

Community
  • 1
  • 1
JustOnUnderMillions
  • 3,741
  • 9
  • 12
0
You could use the DateTime() and DateInterval Classes to achieve that without the need for long, manual calculations as show below.

NOTE: This assumes that $row['timeStart'] and $row['timeEnd'] are valid Date-Formats Strings like 2016-07-23 08:15:30 and also that $row['timeBreak'] is the Number of Minutes for the break say: (30 or 60, for example).
    <?php while($row = $approvedTimes->fetch()){ ?>
        <tr>
            <?php
                $start      = new DateTime($row['timeStart']);
                $end        = new DateTime($row['timeEnd']);
                $breakTime  = new DateInterval('PT'. $row['timeBreak'] .'M');

                // SUBTRACT THE BREAK TIME (IN MINUTES) FROM THE WORK-END TIME: $end  
                $end        = $end->sub($breakTime);

                // GET THE DIFFERENCE BETWEEN WORK-START TIME:$start & WORK-END TIME: $end  
                $timeDiff   = $end->diff($start);

                // BUILD A STRING TO REPRESENT THE DIFFERENCE USING SOME DateInterval METHODS.  
                $strDiff    = $timeDiff->h . " Hours, " . $timeDiff->i . " Minutes &amp; " . $timeDiff->s . " Seconds";
            ?>
            <td><?php echo $row['userFirst'] . " ". $row['userLast'];  ?></td>
            <td><?php echo $row['companyName'];  ?></td>
            <td><?php echo date('Y-M-d', strtotime($row['timeDate']));  ?></td>
            <td><?php echo date("h:i a", strtotime($row['timeStart']));  ?></td>
            <td><?php echo date("h:i a", strtotime($row['timeEnd']));   ?></td>
            <td><?php echo $row['timeBreak'] . " min."; ?></td>
            <td><?php echo $strDiff; /* ECHO OUT THE TOTAL HOURS WORKED...*/  ?></td>
        </tr>
    <?php } ?>
Poiz
  • 7,611
  • 2
  • 15
  • 17
-1

Subtract the full timestamps from one another so that the strtotime sees the date change.

VikingBlooded
  • 884
  • 1
  • 6
  • 17