2014-09-30 138 views
1

我对此有一段时间了。我一直在寻找,我看到不同的东西,我可以使用像CAST,date() function,SUM,Javascript数学等问题是我不知道哪一个使用或哪一个我会用在我的情况。我越是阅读不同的线索和“解决方案”,我就越困惑于如何将其应用于我的需求。我有一个简单的时间表在我的数据库中称为“时间表”在它我有列entry_id(AI),user_id,名字,姓氏,冲(日期时间)和评论(进出)。我已经在PHP中做了一个简单的查询来将用户时间表引入下面的HTML表格中。 timesheet table HTML在while语句中计算时间表中的时间PHP/MYSQL

在我的PHP查询中,我运行了一条WHILE语句来遍历记录。因为我正在使用一个循环,所以我不知道如何计算给出该时间段的总工作时间的时间。

这里是我的查询:

$timesheet = mysqli_query($connect, "SELECT DATE_FORMAT(punch, '%Y/%m/%d') AS punch_date, DATE_FORMAT(punch, '%H:%i:%s') AS punch_time, comment 
        FROM timesheet WHERE user_id = '$employee' AND punch BETWEEN '$fromDate' AND '$toDate'") or die('Error: ' . mysqli_error($connect)); 

if(mysqli_num_rows($timesheet)>0){ 
    $output = '<table class="table table-striped"> 
        <thead> 
        <tr> 
         <th>Date</th> 
         <th>Time</th> 
         <th>Status</th> 
        </tr> 
        </thead> 
        <tbody>'; 
    while ($row = mysqli_fetch_array($timesheet, MYSQL_ASSOC)) { 

     $date = $row['punch_date']; 
     $time = $row['punch_time']; 
     $status = $row['comment']; 

     $output .= '<tr> 
         <td>' . $date . '</td> 
         <td '; 
     if($status === 'In'){ 
      $output .= 'class="in-time"'; 
     } elseif($status === 'Out') { 
      $output .= 'class="out-time"'; 
     } 

     $output .= '>' . $time . '</td> 
         <td>' . $status . '</td> 
        </tr>'; 

    } //end while 

    $output .= '</tbody> 
       </table>'; 


    echo $output; 

} //end if mysqli_num_rows 

它甚至可以计算在一个循环的时间?如果是的话,你会介意给出一个例子,而不是你能指出我在哪里/我做什么的方向是错误的?

UPDATE: 每次进入都有或进或出的状态,所以我需要获得各组进出的差异,然后添加在一起,得到的时间总数的工作为时间段。归根结底,这是我需要完成(见红色总在表的底部) Hopeful outcome

的这里是数据库表“时间表” database

回答

2

的快照,而()之前,我们必须决定是否使用所有行来计算总时间。

因为这里有一个问题。如果用户已经登录但尚未注销,该怎么办?

在这种情况下,我们不会使用最后一次登录时间。所以:

$iterations = mysqli_num_rows($timesheet) % 2 == 0 ? mysqli_num_rows($timesheet) : mysqli_num_rows($timesheet) - 1; 

在你的while循环中,将punch_time转换为秒。

while() { 

... 

$parts = explode(':', $row['punch_time']); 

$seconds = (int) $parts[0] * 3600 + (int) $parts[1] * 60 + (int) $parts[2]; 

if($status === 'In'){ 

    $output .= 'class="in-time"'; 

    $total -= $iterations ? $seconds : 0; // Subtract from total when in and there is out coming 

} else if($status === 'Out') { 

    $output .= 'class="out-time"'; 

    $total += $seconds; // Add to total when out 
} 

... 

$iterations--; 

} 

的循环,可以改变秒到你想要的格式后,通过使用

$hours = floor($total/3600); 

$minutes = floor(($total/60) % 60); 

$seconds = $total % 60; 

echo "$hours:$minutes:$seconds"; 
+0

哇,一为creativetity,谢谢!我有一个快速的问题 - 当使用上面的图像作为参考,当我回声$总数(仍然以秒为单位)时,我得到202851秒,当它转换成小时时,它会达到56.3475小时。所以在某处转换是错误的。在图像中,如果我手工计算数据,它大致会显示总共15分钟的工作时间,这将转换为0.25小时。那个问题在哪里? – Derek 2014-09-30 13:10:24

+0

你是对的!计算错了。我认为这会做到这一点。你的情况是56:16:51。 – tliokos 2014-09-30 13:23:06

+0

非常感谢您的帮助,我发现您的代码将添加总时间条目并将结果作为总数。我已经更新了这个问题,希望能够更清楚地解释我的问题。希望这会对预期结果产生一些影响,迄今为止,您一直非常有帮助。每个条目都有一个In或Out的状态,我需要做的是找出每个In和Out条目之间的时间差异,然后我将这些条目加在一起以获得在这个时间段内工作的总时间,以小时为单位。 – Derek 2014-09-30 15:14:51