php - Displaying correct difference between 2 times -


i'm having bit of issue getting hours worked display correct hours worked.

here code 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 notice second record outputting -17.50 hours hours worked should 6.5 hours. believe problem is seeing both times in seconds i'm unsure of how able change display correct hours worked based on start , end time.

this table output:

time table error

you use datetime() , dateinterval classes achieve without need long, manual calculations show below.

note: assumes $row['timestart'] , $row['timeend'] valid date-formats strings 2016-07-23 08:15:30 , $row['timebreak'] number of minutes break say: (30 or 60, 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 break time (in minutes) work-end time: $end                   $end        = $end->sub($breaktime);                  // difference between work-start time:$start & work-end time: $end                   $timediff   = $end->diff($start);                  // build string represent difference using 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 total hours worked...*/  ?></td>         </tr>     <?php } ?> 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -