Jason Pruim wrote:
So I'm trying to figure out how to do a little math in php to add up the
number of hours that people have worked (Still on the timecard for
anyone following along at home)
I have it inserting time in and timeout as timestamps into a MySQL
database, from there, I use a while loop to display the total hours for
the day which works perfectly, now though, they want it to total for the
week...
I think what I need to do is create an array of my total time for the
day then add that up in another loop to display the total... That seems
like overkill to me though... is there a better way to do it?
Here is the relevant part of the code:
while ($row = mysql_fetch_assoc($result)){
$timein = date("m/d/y h:i:s", $row['timein']);
$timeout = date("m/d/y h:i:s", $row['timeout']);
$totalday = ($row['timeout'] - $row['timein']);
$totalday = $totalday/60/60;
$totalday = round($totalday, 2);
$totalweek = ?!@?#!@#?!@#!$%^!23?!@#!@#?!@#?#$&*^
echo <<<ADMIN
<tr>
<td>{$row['Name']}</td>
<td>{$timein}</td>
<td>{$timeout}</td>
<td>{$totalday}</td>
</tr>
ADMIN;
}
echo "</table>
</div>
</body>";
--
How about having mysql do all the tedious stuff?
SELECT TIMESTAMPDIFF(HOUR, timein, timeout) FROM table WHERE
WEEK(timein) = $someWeek;
would give you the amount of hours between 2 time-values.
Then you could SUM it all up, group by user and return that.
SELECT SUM(timeout-timein) as secs, SUM(timeout-timein)/3600 as hours,
userid FROM table WHERE timein >= $START_OF_WEEK AND timeout <=
$END_OF_WEEK GROUP BY userid;
simple as that :)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php