What I want:- 我想根据多天的工作时间计算员工的总工作时间 .

My problem 总工作时间($ total_hours)无效 .

我的模特

class Attendence扩展AppModel {function add($ data){
if(!empty($ data)){
$这 - >创建();
if($ this-> save($ data)){
返回true;

}
}
}

function fetchdata(){
return $ this-> find('all',array('conditions'=> array('Attendence.date'>'2014-04-01',
'AND'=> array('Attendences.date'<'2014-04-21'),

)));

}
}

我的控制器

class EmployeesController扩展AppController {public $ uses = array('Employee','Attendence','InsertDate');
公共函数add()
{

如果($这个 - >员工 - >添加($这个 - >请求 - >数据)==真){
$这 - >重定向(阵列( '动作'=> '索引'));
}

}

public function index(){
$这个 - >设置( '雇员',$这个 - >员工 - >取());
$这 - >置( '勤',$这 - > Attendence-> fetchdata());
$这 - >集( '日期',$这 - > InsertDate-> fetchdate());
}
}

我的观点

<div class="index">
<table>
  <thead>
    <th>Num</th>
    <th>Employee</th>
    <th>Salary/Hour</th>
    <th>Start Date</th>
    <th>End Date</th>
    <th>Total Hour</th>
    <th>Total Salary</th>
    </thead>

<?php
$id = 0;
foreach($employees as $e):?>
 <? $id++ ?>

    <tr>
        <td><?php echo $e{'Employee'}{'id'} ?></td>
        <td><?php echo $e['Employee']['firstname'], $e['Employee']['lastname'] ?></td>
        <td style="text-align:center"><?php echo $e['Employee']['salary'] ?></td>'


<?php  foreach($dates as $d):?>

            <td><?php echo $d['InsertDate']['start_date'] ?></td>
        <td><?php echo $d['InsertDate']['end_date'] ?></td>





    <?php
        $total_hours = 0;
        foreach ($attendence as $et){
            $ts1 = strtotime($et['Attendence']['in_time']);
            $ts2 = strtotime($et['Attendence']['out_time']);
            $diff = abs($ts1 - $ts2) / 3600;
            $total_hours += number_format($diff,2);

          }
          //Total hours

          echo '<td style="text-align:center">'.$total_hours.'</td>';

          //Total Salary
          echo '<td style="text-align:center">'.$total_hours*$e['Employee']['salary'].'</td>';



          ?>
          <?php endforeach; ?>


          <?php endforeach; ?>

          </tr>
</table>
</div>

我的程序员朋友给了我一个解决方案 . 但我不知道如何在CAKEPHP中实施

我也在更新解决方案

看看这里:

$ total_hours = 0; foreach($ attendence as $ et){$ ts1 = strtotime($ et ['Attendence'] ['in_time']); $ ts2 = strtotime($ et ['Attendence'] ['out_time']); $ diff = abs($ ts1 - $ ts2)/ 3600; $ total_hours = number_format($ diff,2); }

代码显示有一个数组 . 数组包含(每个点的考勤ID和in_time和out_time) . 重要的是你应该检查你如何填充这个数组 . 在你通过$ employees数组生成表的上述foreach中,你在这里有(employee_id)名字(id)所以你应该在你的视图中写一个新的查询!!!在你的第一个foreach中间和第二个foreach之前在此之前:

$ total_hours = 0

您必须编写查询并从DB获取数据,如下所示:

//SELECT * FROM attendences
    WHERE attendences.date > '2014-04-23' AND attendences.date < '2014-04-30'
    AND id=$e['Employee']['id'] // is your employee_id in your first array.

因此,当您获取数据时,您有一个名为“$ attendence”的新数组然后,您的第二个foreach(计算工资和总小时数)应该正常工作