如何计算员工一周内的总小时数和分钟数

时间:2014-10-17 11:27:19

标签: php mysql

我正在查询从数据库中获取员工的日期和时间。然后我计算他们一天工作的总小时数和分钟数。目前我陷入困境,我需要计算员工一周工作的总小时数。请帮忙,我如何计算员工一周工作的总小时数和分钟数。

代码

<?php
    $sql = "SELECT * FROM records WHERE records_status = 'finished' AND record_created > DATE_SUB('2014-10-19', INTERVAL 7 DAY)";
    $query = $db->SELECT($sql);
?>
<table width="39%" border="1">
  <tbody>
    <tr>
      <td style="padding: 8px;"><strong>Day</strong></td>
      <td style="padding: 8px;"><strong>Total Hours</strong></td>
    </tr>
    <?php
            $tally = "";
            foreach( $db->FETCH_OBJECT() as $row ){
                $record_sign_in = $row->record_sign_in;
                $record_sign_out = $row->record_sign_out;
                $record_created = $row->record_created;
                $time1 = date("H:i", strtotime($record_sign_in) );
                $time2 = date("H:i", strtotime($record_sign_out) );
                $record_created = date("l", strtotime($record_created) );
                $day = $record_created;
                list($hours, $minutes) = explode(':', $time1);
                    $startTimestamp = mktime($hours, $minutes);
                list($hours, $minutes) = explode(':', $time2);
                    $endTimestamp = mktime($hours, $minutes);
                $seconds = $endTimestamp - $startTimestamp;
                $minutes = ($seconds / 60) % 60;
                $hours = floor($seconds / (60 * 60));

                $tally = "What to do here?";
        ?>
    <tr>
      <td style="padding: 8px;"><?php echo $day; ?></td>
      <td style="padding: 8px;"><?php echo $hours; ?> hrs <?php echo $minutes; ?> min</td>
    </tr>
    <?php } ?>
    <tr>
      <td style="padding: 8px;">Total</td>
      <td style="padding: 8px;"></td>
    </tr>
  </tbody>
</table>

视觉表就是这样的。

enter image description here

2 个答案:

答案 0 :(得分:1)

那么你可以先删除大量不必要的中间变量创建,然后你需要做的就是每次通过循环将$seconds添加到$tally然后转换$tally几分钟和几秒钟就像你已经在每一天一样。

<?php
    $sql = "SELECT * 
            FROM records 
            WHERE records_status = 'finished' 
              AND record_created > DATE_SUB('2014-10-19', INTERVAL 7 DAY)";
    $query = $db->SELECT($sql);
?>
<table width="39%" border="1">
  <tbody>
    <tr>
      <td style="padding: 8px;"><strong>Day</strong></td>
      <td style="padding: 8px;"><strong>Total Hours</strong></td>
    </tr>
    <?php
            $tally = 0;
            foreach( $db->FETCH_OBJECT() as $row ){

                $time1 = date("H:i", strtotime($row->record_sign_in) );
                $time2 = date("H:i", strtotime($row->record_sign_out;) );
                $day = date("l", strtotime($row->record_created) );

                list($hours, $minutes) = explode(':', $time1);
                    $startTimestamp = mktime($hours, $minutes);
                list($hours, $minutes) = explode(':', $time2);
                    $endTimestamp = mktime($hours, $minutes);

                $seconds = $endTimestamp - $startTimestamp;
                $minutes = ($seconds / 60) % 60;
                $hours = floor($seconds / (60 * 60));

                $tally += $seconds;
        ?>
    <tr>
      <td style="padding: 8px;"><?php echo $day; ?></td>
      <td style="padding: 8px;"><?php echo $hours; ?> hrs <?php echo $minutes; ?> min</td>
    </tr>
    <?php } ?>
    <tr>
      <td style="padding: 8px;">Total</td>
<?php
    $minutes = ($tally / 60) % 60;
    $hours = floor($tally / (60 * 60));
?>
      <td style="padding: 8px;">
         <?php echo $hours; ?> hrs <?php echo $minutes; ?> min
      </td>
    </tr>
  </tbody>
</table>

答案 1 :(得分:0)

使用以下顺序:

  1. 使用strtotime将时间转换为秒(纪元)。
  2. 做数学
  3. 使用date或gmdate
  4. 转换为人格式

    不需要中间变量,列表,mktime和爆炸。

    工作原件:https://eval.in/207141

    解决方案:https://eval.in/207145

    <pre>
    <?php
    // test vector
    $dbs[] = ['record_created'=>'20141201', 'record_sign_in'=>'09:30', 'record_sign_out'=>'18:30'];
    $dbs[] = ['record_created'=>'20141202', 'record_sign_in'=>'09:30', 'record_sign_out'=>'18:30'];
    $dbs[] = ['record_created'=>'20141203', 'record_sign_in'=>'09:25', 'record_sign_out'=>'18:30'];
    $dbs[] = ['record_created'=>'20141204', 'record_sign_in'=>'09:35', 'record_sign_out'=>'18:30'];
    $dbs[] = ['record_created'=>'20141205', 'record_sign_in'=>'09:50', 'record_sign_out'=>'18:30'];
    $dbs = json_decode (json_encode ($dbs), FALSE); // convert to object for compatibility with code below
    ?>
    <table width="39%" border="1">
      <tbody>
        <tr>
          <td style="padding: 8px;"><strong>Day</strong></td>
          <td style="padding: 8px;"><strong>Total Hours</strong></td>
        </tr>
        <?php
                $tally = 0; // total time
                foreach( $dbs as $row ){ // statement slightly modified for test purpose
                    // convert to seconds
                    $created = strtotime($row->record_created);
                    // conversion and math
                    $seconds = strtotime($row->record_sign_out) - strtotime($row->record_sign_in); // time difference in seconds
                    $tally += $seconds;
                    // human format
                    $day = date("l", $created);
                    $hours = gmdate("H", $seconds);
                    $minutes = gmdate("i", $seconds);
            ?>
        <tr>
          <td style="padding: 8px;"><?php echo $day; ?></td>
          <td style="padding: 8px;"><?php echo $hours; ?> hrs <?php echo $minutes; ?> min</td>
        </tr>
        <?php } 
            // human format
            $hours = floor($tally / 3600);
            $minutes = gmdate("i", $tally);
        ?>
        <tr>
          <td style="padding: 8px;">Total</td>
          <td style="padding: 8px;"><?php echo $hours; ?> hrs <?php echo $minutes; ?> min</td>
        </tr>
      </tbody>
    </table>