Jump to content

Recommended Posts

Ok, so I am writing a program to log hours worked on a project. There are clock-in times, and clock out times, then I used a php operator to subtract the two and find the hours. This works fine.

 

BUT...

 

I have been searching for a way to then add all of these hours together and get a total hours.

 

Here is the code for the table:

echo "<table border='1'><tr><th>Date</th><th>Time in</th><th>Time out</th><th>Hours</th><th>Notes</th>";

 

$query3 = mysql_db_query($db, "SELECT * FROM times WHERE clientID='$ID' ORDER BY date");

 

while($result = mysql_fetch_array($query3)) {

 

$date= $result["date"];

$IN = $result["timeIN"];

$OUT = $result["timeOUT"];

$notes = $result["notes"];

 

$hours = $OUT-$IN;

 

echo "<tr><td>$date</td>";

echo "<td>$IN</td><td>$OUT</td>";

echo "<td>$hours</td>";

 

echo "<td>$notes</td></tr>";

 

}

 

let me know of either a way to get a total hours from here, or a better way to get the same results, and the total.

 

thanks

Adjusting the code you already have

<?php
echo "<table border='1'><tr><th>Date</th><th>Time in</th><th>Time out</th><th>Hours</th><th>Notes</th>";

$query3 = mysql_db_query($db, "SELECT * FROM times WHERE clientID='$ID' ORDER BY date");

$total_hours = 0;
while($result = mysql_fetch_array($query3)) {

    $date= $result["date"];
    $IN = $result["timeIN"];
    $OUT = $result["timeOUT"];
    $notes = $result["notes"];

    $hours = $OUT-$IN;

    echo "<tr><td>$date</td>";
    echo "<td>$IN</td><td>$OUT</td>";
    echo "<td>$hours</td>";

    echo "<td>$notes</td></tr>";

    $total_hours += $hours;
}

echo "Total hours : $total_hours";
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.