我是PHP的新手,对CakePHP来说更新,这是我的代码所写的,但我认为我的问题在于我的一些标准PHP代码。
好的,现在这个代码大部分都在工作,但是当我选择的用户同时花费超过一天时,它似乎给了我一个错误的结果!
好的,所以它的目的是为所选用户提供多年的服务,它就可以了(我认为,没有任何问题)。然后,根据服务年限,我根据服务年限设定了病假权利,整天和半天。
一旦我完成了这项工作,代码应该让选定的用户感到厌倦,然后取消他们使用的权利,首先是完整的,然后当它是空的时候将它从一半中取出。但就像我说的那样,当用户连续休息一天以上时,它仍然只有一天起飞?
这可能是我开始我的foreach循环并进行计数的地方,不确定这是否正确?
请帮忙??
function sickDay($id = null) {
if($this->CURRENT_USER['User']['role_id'] > 3) { //directors and admins
/*if(empty($this->data) || !isset($this->data['Holiday']['id'])) {
} else {*/
//Caculate if it's a full pay, half pay or nothing
//$data = $this->data['Holiday'];
//Get Holidaytypes
$types = $this->Holiday->find(
'all',
array(
'conditions' => array(
'Holiday.holidaystype_id' => 3,
'Holiday.user_id' => $id
)
)
);
//Get starting date
$contracts = $this->Holiday->User->Contract->find(
'all',
array(
'conditions' => array(
'Contract.user_id' => $id //$data['user_id']
),
'order' => array(
'Contract.startson' => 'ASC'
)
)
);
//Get How Many sick days
foreach ($types as $key => $value) {
$typesDataEnds = strftime ("%u-%d-%Y", $types[$key]['Holiday']['endson']);
$typesDataStarts = strftime ("%u-%d-%Y", $types[$key]['Holiday']['startson']);
$SickTotal = count($typesDataEnds - $typesDataStarts);
//echo $SickTotal;
//Get Contract Start & End Dates
$start = array_shift($contracts);
$end = array_pop($contracts);
$endDate = $end['Contract']['endson'];
$startDate = $start['Contract']['startson'];
if (empty($endDate)) {
$endDate = time('now');
}
if (!empty($startDate)) {
$SortEnd = strftime("%Y", $endDate);
$SortStart = strftime("%Y", $startDate);
$YearsService = $SortEnd - $SortStart;
if ($YearsService <= 1) {
$SetFullEntitlement = 5;
$SetHalfEntitlement = 5;
//echo 'one year';
} elseif ($YearsService <= 2) {
$SetFullEntitlement = 10;
$SetHalfEntitlement = 10;
//echo 'two years';
} elseif ($YearsService <= 5) {
$SetFullEntitlement = 20;
$SetHalfEntitlement = 20;
//echo 'up to five years';
} elseif ($YearsService >= 6) {
$SetFullEntitlement = 30;
$SetHalfEntitlement = 30;
//echo 'five years or more';
} else {
$SetFullEntitlement = 0;
$SetHalfEntitlement = 0;
//echo 'no sick pay';
}
} else {
$YearsService = 0;
//echo 'Sorry No Start Date For You Found!';
}
while ($SickTotal > 0) {
if ($SetFullEntitlement != 0) {
$SetFullEntitlement--;
} elseif ($SetHalfEntitlement != 0) {
$SetHalfEntitlement--;
}
$SickTotal--;
}
echo 'FullPay:';
echo $SetFullEntitlement;
echo '<br/><br/>Halfpay:';
echo $SetHalfEntitlement;
}
//debug($SickTotal);
die();
//$this->render('/artists/holidayslist');
//} //End For if empty check
} // End for if CURRENT_USER
} //End of Function
更新
我明白你说了什么,是的,我现在知道你用它做我需要它做错了。现在我已经完成了你所说的但是我为一个选定的用户找回了0,我知道应该有两个用户?
foreach ($types as $key => $value) {
$typesDataEnds = strtotime($types[$key]['Holiday']['endson']);
$typesDataStarts = strtotime($types[$key]['Holiday']['startson']);
//$typesDataEnds = strftime ("%Y-%m-%d", $types[$key]['Holiday']['endson']);
//$typesDataStarts = strftime ("%Y-%m-%d", $types[$key]['Holiday']['startson']);
$sickTotal = floor(($typesDataEnds - $typesDataStarts) / 86400);
echo 'testing: ';
debug($sickTotal);
die();
does more.....
} //end
我做错了吗?
非常感谢您的帮助
格伦。
答案 0 :(得分:0)
我在计算病假的行中看到了三个问题:
您在计算之前格式化日期(格式错误,请参阅docs了解%u
的含义)。您需要数值来进行计算。如果日期采用strtotime
可解析的格式:
$end = strtotime($types[$key]['Holiday']['endson']);
$start = strrotime($types[$key]['Holiday']['startson']);
然后你尝试减去两个字符串。那不行。但是如果你使用strtotime
它确实有效,因为那时你正在对整数进行操作:
$sickTotal = floor(($end - $start) / 86400);
// 86400 seconds in a day
请勿在结果上使用count
。这是对于数组和对象,当你向其传递任何其他东西时,它返回1
(这解释了你看到的错误结果;你传递了一个字符串)。
我为计算构建了一个working demo。