I am very new to PHP and even more new to CakePHP, which is what my code is writen for but I think my problem is with some of my standard PHP code.
OK, now this code is mostly working but it seems to give me a wrong result when my selected user takes more than one day of at the same time!
OK, so what it is meant to do is to get the years of service for the selected user, it does that OK (i think, no problems at lest). Then from years of service I set sick entitlement, full days and then half days based on years of service.
Once I have done that the code should get the selected users taken sick and with that take off the what they have taken with the entitlement, 1st the full, then when that is empty take it off the half. But like I said when the user takes more than one day off in a row, it still only takes off one day?
It might be where I start my foreach loop and do the count, not sure if that is right?
Please help??
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
Update
I understand what you have said and yes, I do now know %u was wrong to use for what I need it to do. Now I have done what you said but I get back 0 for a selected user, which I know should have two?
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
Have I done something wrong?
Many Thanks for the help
Glenn.
I see three problems in the lines where you calculate the sick days:
You’re formatting the dates before the calculation (and the formatting is wrong, see the docs for the meaning of
%u). You need numeric values for the calculation. This should work provided the dates are in a format parseable bystrtotime:Then you try to subtract two strings. That won’t work. But if you use
strtotimeit does work, because then you’re operating on integers:Do not use
counton the result. That’s for arrays and objects, when you pass anything else to it, it returns1(which explains the wrong results you’re seeing; you’re passing a string).I’ve build a working demo for the calculation.