私が取り組んでいるプログラムでは、ユーザーが各タスクの作業にどれくらいの時間を費やしているかを示す表があります。 Time On Task
欄の総計を表示したいと思います。Mysql SUM()対PHPの通常の加算演算
tfoot
はwhileステートメントの外にあるため、私は総計を計算するために別のクエリを使用しています。このPenは、今のように見えます。
ご覧のとおり、$grandtotal
の合計が$tasktime
の合計と等しくないという問題があります。どうすればこの作品を作れますか? PHPで
グランド合計クエリ
$stots ="SELECT *, SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(te.date_end, te.date_start))))
AS Duration FROM taskentries";
$resultots=mysqli_query($db,$stots);
$totaltime =mysqli_fetch_assoc($resultots);
$grandtotal = $totaltime['Duration'];
表
<table>
<thead>
<th>Task</th>
<th>Date Start</th>
<th>Date End</th>
<th>Time On Task</th>
</thead>
<tfoot>
<tr>
<td th='Task'></td>
<td th='Date Start'></td>
<td th='Date End'>Grand Total</td>
<td th=''>
<?php
if($grandtotal < 0){
echo '0';
}else{
echo $grandtotal;
}?>
</td>
</tr>
</tfoot>
<tbody>
<?php
$sql="SELECT * FROM taskentries";
$result=mysqli_query($db,$sql);
while($taskentry=mysqli_fetch_array($result)){ ?>
<tr>
<td th='Task'><?=$taskentry['task'];?></td>
<td th='Date Start'><?=$taskentry['date_start'];?></td>
<td th='Date End'><?=$taskentry['date_end'];?></td>
<td th='Time On Task'>
<?php
$date_start = date("h:i:sa",strtotime($taskentry['date_start']));
$date_end = date("h:i:sa",strtotime($taskentry['date_end']));
$tasktime = $date_end - $date_start;
if($tasktime < 0){
echo '0';
}else{
echo $tasktime;
}?>
</td>
</tr>
<?php };?>
</tbody>
</table>
'$ total + = $ date_end - $ date_start;'私が通知を消した '+'を削除すると未定義の変数 '$ total'が得られます。あなたのコードの結果は整数です。最初の行は現在7200、2行目は6406です。 – Clark
ループの前に変数を0に初期化する必要があります。 – Barmar
おそらく、 'DateInterval'クラスを使うべきでしょう。 – Barmar