从mysql数据库中添加小时数,然后通过PHP将它们添加到HTML表中

时间:2012-04-12 08:15:10

标签: php mysql html-table

你可能会说我在使用PHP的mysql方面很差劲!所以这是我的问题:

我有一个名为 TimeRecords 的mysql表,其中包含以下列:ID,Date,StartTime,EndTime,Break和Location。

我有一个表格,使用此SELECT

显示该信息
$result = mysql_query("
        SELECT * FROM TimeRecords
        WHERE Date
        BETWEEN '{$CurrentYear}-{$CurrentMonth}-1'
        AND '{$CurrentYear}-{$CurrentMonth}-31'
    ");

这个代码:

<table>
    <tr>
        <th>Location:</th>
        <th>Date:</th>
        <th>Start:</th>
        <th>End:</th>
        <th>Break?</th>
        <th>Total Hours</th>
    </tr>

    <?php 
        while($row = mysql_fetch_array($result)) {

            echo "<tr>";
                echo "<td>". $row['Location'] . "</td>";
                echo "<td>". $row['Date'] . "</td>";

                echo "<td>" . date("h:i A", ($row['StartTime'])) . "</td>";
                echo "<td>" . date("h:i A", ($row['EndTime'])) . "</td>";

                echo "<td>";

                if ($row['Break']==1){
                    echo "Yes";
                }
                else {
                    echo "No";
                }
                echo "</td>";

            echo "</tr>";
        }


?>
</table>

如何在表格中创建另一个会减去EndTime和StartTime以获得当天的总工作时数?

根据我的理解,我可以使用此SELECT获取该信息:

$TimeWorked = mysql_query("
        SELECT ((EndTime - StartTime)/60/60) AS TimeWorked
        FROM TimeRecords
        ")

但是我不确定我应该做什么来在循环中显示它。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

尝试:

$result = mysql_query("
        SELECT a.*, ((a.EndTime - a.StartTime)/3600) AS TotalHours FROM TimeRecords a
        WHERE Date
        BETWEEN '{$CurrentYear}-{$CurrentMonth}-1'
        AND '{$CurrentYear}-{$CurrentMonth}-31'
    ");

看看它是否适合你。

答案 1 :(得分:0)

$starttime = $row['StartTime']
$endtime = $row['EndTime']
echo "<td>" . date("h:i A", ($starttime)) . "</td>";
echo "<td>" . date("h:i A", ($endtime)) . "</td>";
echo "<td>" . ($endtime - $starttime)/3600 . "</td>";

我不完全确定我知道你的意思但这对你有用吗?