【问题标题】:Display total hours from last seven days显示过去 7 天的总小时数
【发布时间】:2014-04-10 22:20:29
【问题描述】:

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

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

【问题讨论】:

    标签: php date timestamp echo


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      相关资源
      最近更新 更多