【问题标题】:How to calculate total hours and minutes in a week of an employee如何计算员工一周的总小时数和分钟数
【发布时间】:2014-10-17 11:27:19
【问题描述】:

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

代码

<?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>

这就是可视化表格的样子。

【问题讨论】:

    标签: php mysql


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      使用以下顺序:

      1. 使用 strtotime 将时间转换为秒(纪元)。
      2. 算一算
      3. 使用日期或 gmdate 转换为人类格式

      不需要中间变量、list、mktime和explodes。

      工作原件: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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-02-16
        • 1970-01-01
        • 2017-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多