显示过去七天的总小时数

时间:2014-04-10 22:20:29

标签: php date timestamp echo

我正在编辑一个时钟系统供员工上班和下班,我正在尝试显示过去七天的总小时数,但是代码有点混乱,我无法弄清楚如何接近那个。有人想给我一些帮助吗?

        <?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 mins\n";
    }
?>

1 个答案:

答案 0 :(得分:0)

据我所知,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 seconds\n";