我正在一个工作场所工作的网站上工作。小时数是根据它们进入(开始)和退出(结束)的时间计算的。结束 - 开始=总计。我已成功计算出开始和结束之间的差异。 start,end和total将插入到DB中。 " Shift total"来自db中的总数的SQL SUM。最后一个foreach语句是有问题的代码。我想格式化" Shift total"为00:00:00所以它将显示为00:01:26而不是126.我已经尝试了#34;。 $ row [' SUM(总计)'] +' 00:00:00' 。 "但它没有用。关于我应该怎么做的任何建议?提前致谢。我先将结果粘贴到代码中。
____开始____________端____________总
10:44:46 AM 10:44:49 AM 00:00:03
10:50:12 AM 10:50:14 AM 00:00:02
10:50:27 AM 10:50:32 AM 00:00:05
11:12:02 AM 11:13:18 AM 00:01:16
Shift total --> 126
$sql3 = "SELECT date, startTime, endTime, total FROM timeSheet WHERE userName='$user_name'";
$sql4 = "SELECT SUM(total) FROM timeSheet WHERE userName='$user_name'";
echo "<table border='1'>
<tr>
<th>date</th>
<th>start</th>
<th>end</th>
<th>total</th>
</tr>";
foreach ($conn->query($sql3) as $row) {
$start = explode (" ", $row['startTime']);
$end = explode(" ", $row['endTime']);
if ($start[1] == '00:00:00'){
$start[1] = '0';
}else if ($end[1] == '00:00:00'){
$end[1] = '0';
}else{
$start[1] = date("h:i:s A", strtotime($start[1]));
$end[1] = date("h:i:s A", strtotime($end[1]));
}
echo "<tr>
<th>" . $row['date'] . "</th>
<th>" . $start[1] . "</th>
<th>" . $end[1] . "</th>
<th>" . $row['total'] . "</th>
</tr>";
}
foreach ($conn->query($sql4) as $row) {
echo "<tr>
<th></th>
<th></th>
<th>SUM</th>
<th>". $row['SUM(total)'] . "</th>
</tr>";
}
echo "</table>";
答案 0 :(得分:0)
您可以使用TIME
函数执行此操作:
$sql4 = "SELECT TIME(SUM(total)) AS totalSum FROM timeSheet WHERE userName='$user_name'";
//....
foreach ($conn->query($sql4) as $row) {
echo "<tr>
<th></th>
<th></th>
<th>SUM</th>
<th>". $row['totalSum'] . "</th>
</tr>";
}
<强>提示:强>
实际上,您不需要数据库中的total
列。你可以使用即
SELECT date, startTime, endTime, TIMEDIFF(startTime, endTime) AS total FROM timeSheet WHERE userName='$user_name'"
和
SELECT TIME(SUM(TIMEDIFF(startTime, endTime))) AS totalSum FROM timeSheet WHERE userName='$user_name'
有关详细信息,请查看:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff