gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:38:36 25 4
gpt4 key购买 nike

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

代码

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

最佳答案

那么,您可以首先删除大量不必要的中间变量创建,然后您需要做的就是每次通过循环将 $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>

关于php - 如何计算员工一周的总小时数和分钟数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26424055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com