在 php mysql 中额外提供一小时的时间总和

问题描述 投票:0回答:2

我正在尝试计算时间总和。一切正常,但总时间显示多出一小时。例如,如果时间总和需要为 20:23,则显示 21:23。我不知道为什么加起来要多加一小时。这是我的代码:

 $sql = "SELECT *, TIME_TO_SEC(wrktime) as bbb1 FROM tmmach ORDER BY ddd DESC LIMIT 30";

if ($result = mysqli_query($link, $sql)) {
    if (mysqli_num_rows($result) > 0) {
        echo '<table class="table table-bordered table-striped">';
        echo "<thead>";
        echo "<tr>";
        echo "<th>Emp-Id</th>";
        echo "<th>Name</th>";
        echo "<th>Company</th>";
        echo "<th>Date</th>";
        echo "<th>worktime</th>";
     echo "<th>worktime</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";

        $totalWorkTime = 0; // Initialize the variable for sum

        while ($row = mysqli_fetch_array($result)) {
            echo "<tr>";
            echo "<td>" . $row['empid'] . "</td>";
            echo "<td>" . $row['empname'] . "</td>";
            echo "<td>" . $row['company1'] . "</td>";
            echo "<td>" . date('d-M-Y', strtotime($row['date1'])) . "</td>";
 
            echo "<td>" . $row['wrktime'] . "</td>";
        echo "<td>" . $row['bbb1'] . "</td>";

            // Update the sum variable
            $totalWorkTime += $row['bbb1'];
            $hhh = date('H:i',$totalWorkTime);   
  
  
            


            echo "</tr>";
        }

        echo "</tbody>";

        // Display the sum row
        echo "<tfoot>";
        echo "<tr>";
        echo "<td colspan='4'><strong>Total Work Time:</strong></td>";
      echo "<td><strong>" . $hhh . "</strong></td>";
        echo "</tr>";
        echo "</tfoot>";

        echo "</table>";
        // Free result set
        mysqli_free_result($result);
    } else {
        echo '<div class="alert alert-danger"><em>No records were found.</em></div>';
    }


                    }
php mysql sum
2个回答
0
投票

要计算“worktime”列的总和,您可以在循环之前初始化一个变量以累积值并在循环内更新它。这是代码的修改版本,其中包含总和计算:

$sql = "SELECT * FROM timetablw ORDER BY id DESC LIMIT 30";

if ($result = mysqli_query($link, $sql)) {
    if (mysqli_num_rows($result) > 0) {
        echo '<table class="table table-bordered table-striped">';
        echo "<thead>";
        echo "<tr>";
        echo "<th>Emp-Id</th>";
        echo "<th>Name</th>";
        echo "<th>Company</th>";
        echo "<th>Date</th>";
        echo "<th>worktime</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";

        $totalWorkTime = 0; // Initialize the variable for sum

        while ($row = mysqli_fetch_array($result)) {
            echo "<tr>";
            echo "<td>" . $row['empid'] . "</td>";
            echo "<td>" . $row['empname'] . "</td>";
            echo "<td>" . $row['company1'] . "</td>";
            echo "<td>" . date('d-M-Y', strtotime($row['date1'])) . "</td>";
            echo "<td>" . $row['wrktime'] . "</td>";

            // Update the sum variable
            $totalWorkTime += $row['wrktime'];

            echo "</tr>";
        }

        echo "</tbody>";

        // Display the sum row
        echo "<tfoot>";
        echo "<tr>";
        echo "<td colspan='4'><strong>Total Work Time:</strong></td>";
        echo "<td><strong>" . $totalWorkTime . "</strong></td>";
        echo "</tr>";
        echo "</tfoot>";

        echo "</table>";
        // Free result set
        mysqli_free_result($result);
    } else {
        echo '<div class="alert alert-danger"><em>No records were found.</em></div>';
    }
}

此修改包括一个变量

$totalWorkTime
,该变量在循环之前初始化并在循环内更新以累积值。然后,总和将显示在表页脚的新行中。根据需要调整格式。


0
投票

您可以在查询中得到所需的总金额:

SELECT empid, 
       IF(GROUPING(empid, empname, company), 'TOTAL', empname) empname, 
       company, 
       SUM(wrktime) wrktime
FROM timetablw
GROUP BY empid, empname, company WITH ROLLUP
HAVING GROUPING(empid, empname, company) IN (0, 7)
ORDER BY GROUPING(empid, empname, company), empid, empname, company

根据表达式调整

SUM(wrktime)
(取决于
wrktime
数据类型和格式)。

© www.soinside.com 2019 - 2024. All rights reserved.