所以向你简要介绍一下这个问题:
现在考虑到这些细节,我想获得一份 x 员工(固定金额,即3)的清单,这些员工的工资与项目的日期范围相匹配,也插入预算。
我开始按照以下方式执行此操作,但不知道如何继续:
$employees = array(
0 => array(
'id' => 1,
'salary' => 22000
);
);
$startDate = some_date_here;
$endDate = some_date_here;
foreach($employees as &$user) {
$dayRate = round($user['salary'] / 260, 2);
}
修改
使用http://php.net/manual/en/function.date-diff.php#117691,我设法找到项目日期之间的日期,如下所示(代码更新):
function days_diff($d1, $d2) {
$x1 = days($d1);
$x2 = days($d2);
if ($x1 && $x2) {
return abs($x1 - $x2);
}
}
function days($x) {
if (get_class($x) != 'DateTime') {
return false;
}
$y = $x->format('Y') - 1;
$days = $y * 365;
$z = (int)($y / 4);
$days += $z;
$z = (int)($y / 100);
$days -= $z;
$z = (int)($y / 400);
$days += $z;
$days += $x->format('z');
return $days;
}
$startDate = DateTime::createFromFormat('Y-m-d', $startDate);
$endDate = DateTime::createFromFormat('Y-m-d', $endDate);
$projectDays = days_diff($startDate, $endDate);
答案 0 :(得分:1)
我认为您可以通过以下代码执行此操作。
$startDate = some_date_here;
$endDate = some_date_here;
$noOfDays = calculate_using_end-start;
$employeeRate = [];
$noOfEmployeesRequired = 3;
foreach($employees as $index=>$user) {
$dayRate = round($user['salary'] / 260, 2);
$totalRate = $dayRate*$noOfDays;
$employeeRate[$user['id']] = $totalRate;
}
现在我有所有员工费用因此我对它进行排序并找到最便宜的解决方案
sort($employeeRate);
$output = array();
$counter = 0;
foreach($employeeRate as $id=>$emp) {
array_push($output, $id);
$counter++;
if($counter == $noOfEmployeesRequired) {
break;
}
}