显示最近七天的总小时数



我正在编辑一个时钟系统,供员工打卡进出,并试图显示过去七天的总小时数,但代码有点混乱,我很难弄清楚如何处理。有人想帮我吗?

        <?php
    // check if we have a report to generate
    if ($_GET['action'] == "generate") {
        // get name
        $sql = "select name from employees where id = " . $_GET['employee_id'] . " limit 1";
        $result = mysql_query($sql) or die($sql);
        $row = mysql_fetch_assoc($result);
        extract($row);
        // maybe need to edit this to get a better idea of time
        $sql = "select unix_timestamp(time_in) as time_i, unix_timestamp(time_out) as time_o, timediff(time_out, time_in) as time_diff from time where employee_id = " . 
            $_GET['employee_id'] . " and time_out is not null and time_in between '" . $_GET['date_from'] . "' and '" . $_GET['date_to'] . " 23:59:59' and " . 
            "date_format(time_out, '%c%d%Y') = date_format(time_in, '%c%d%Y') order by time_i asc";
        $result = mysql_query($sql) or die($sql);
        echo "<h1>TimeClock Report for $name</h1>n";
        echo "<table cellpadding="4" cellspacing="6">n";
        echo "<tr><th align="center"><u>Date</u></th><th align="center"><u>Clock In Time</u></th><th align="center"><u>Clock Out Time</u></th><th align="center"><u>Total Time</u></th></tr>n";
        $current_time = time();
        $new_time = $current_time;
        while ($row = mysql_fetch_array($result)) {
            echo "<tr>";
            echo "<td align="center">" . date("M jS", $row['time_i']) . "</td>";
            echo "<td align="center">" . date("g:i:s A", $row['time_i']) . "</td>";
            echo "<td align="center">" . date("g:i:s A", $row['time_o']) . "</td>";
            echo "<td align="center">" . $row['time_diff'] . "</td>"; 
            echo "</tr>n";
            $hrs = substr($row['time_diff'], 0, 2);
            $min = substr($row['time_diff'], 3, 2);
            $sec = substr($row['time_diff'], 6, 2);
            $new_time += $sec;
            $new_time += ($min * 60);
            $new_time += ($hrs * 60 * 60);
        }
        echo "</table>n";
        $date_diff = $new_time - $current_time;
        $fullDays = floor($date_diff/(60*60*24));
        $fullHours = floor(($date_diff-($fullDays*60*60*24))/(60*60));
        $fullMinutes = floor(($date_diff-($fullDays*60*60*24)-($fullHours*60*60))/60);
        $fullHours = $fullHours + ($fullDays * 24);
        echo "<strong><u>Total:</u></strong> $fullHours hours $fullMinutes minsn";
    }
?>

据我所知,time_I、time_o和time()php函数都返回unix时间戳。所以这应该工作

    $totalSecondsLast7Days = 0;
    while ($row = mysql_fetch_array($result)) {
        echo "<tr>";
        echo "<td align="center">" . date("M jS", $row['time_i']) . "</td>";
        echo "<td align="center">" . date("g:i:s A", $row['time_i']) . "</td>";
        echo "<td align="center">" . date("g:i:s A", $row['time_o']) . "</td>";
        echo "<td align="center">" . $row['time_diff'] . "</td>"; 
        echo "</tr>n";
        $hrs = substr($row['time_diff'], 0, 2);
        $min = substr($row['time_diff'], 3, 2);
        $sec = substr($row['time_diff'], 6, 2);
        $new_time += $sec;
        $new_time += ($min * 60);
        $new_time += ($hrs * 60 * 60);
        // TO TRACK THE TIME FOR THE LAST 7 DAYS
        // I use time_i here but maybe you want to use time_o
        if ($row['time_i'] + 60*60*24*7 > $current_time)
        {
            $totalSecondsLast7Days += ($sec + $min*60 + $hrs*60*60);
        }
    }

然后你就把它展示成最适合你的样子。

    echo "<strong><u>Last 7 days:</u></strong> $totalSecondsLast7Days secondsn";

最新更新