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

0lvr5msh  于 12个月前  发布在  PHP
关注(0)|答案(2)|浏览(150)

我试图计算时间的总和.一切都工作正常,但时间的总和显示一个小时的额外.例如,如果时间的总和需要20:23,它显示21:23.我不知道为什么它给出了一个小时的额外总和.(我来通过谷歌知道,它可以通过DateTimeImmutable解决,但我不知道如何使用,因为我从来没有这样做过)这里是我的代码:

$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>';
    }

                    }

字符串

l2osamch

l2osamch1#

要计算“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,该变量在循环前初始化并在循环内更新以累计值。然后,总和将显示在表页脚的新行中。根据需要调整格式。

5jdjgkvh

5jdjgkvh2#

您可以在查询中获得所需的总和:

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数据类型和格式)。

相关问题