This topic is locked
[SOLVED]

 Holidaays & Weekends

7/1/2013 8:41:17 AM
PHPRunner General questions
S
Sergej author

I'm trying to calculate days used on vacation - and it works fine when holidays are not due on weekend - in that case - i get same day twice subtracted - once as holiday - once as weekend day. This is code i use in after record added section
global $conn;

$start = new DateTime($values['Od']);

$end = new DateTime($values['Do']);

$end->modify('+1 day');

$interval = $end->diff($start);

$days = $interval->days;

$period = new DatePeriod($start, new DateInterval('P1D'), $end);

$holidays = array('01-01','06-01','31-03','01-04','01-05','30-05','22-06', '25-06','05-08','15-08', '08-10', '01-11', '25-12','26-12');
foreach($period as $dt) {

$curr = $dt->format('D');
// for the updated question

if (in_array($dt->format('d-m-Y'), $holidays))

{

if($curr==Sat|| $curr==Sun){

next();

}

else

{

$days--;
}

}
// substract if Saturday or Sunday

if ($curr == 'Sat' || $curr == 'Sun') {

$days--;

}

}

Can someone help with this...thank you very much!

C
cgphp 7/1/2013

I have made some changes to your code. Please, check the code below.
Let me do an example.
Start date: 2013-01-01

End date: 2013-01-31
Using your code the total days substracted are eight (01-01, 05-01, 12-01, 13-01, 19-01, 20-01, 26-01, 27-01).

$start = new DateTime("2013-01-01");

$end = new DateTime("2013-01-31");

$end->modify('+1 day');

$interval = $end->diff($start);

$days = $interval->days;

$period = new DatePeriod($start, new DateInterval('P1D'), $end);

$holidays = array('01-01','06-01','31-03','01-04','01-05','30-05','22-06', '25-06','05-08','15-08', '08-10', '01-11', '25-12','26-12');
foreach($period as $dt)

{

$curr = $dt->format('D');
//for the updated question

if(in_array($dt->format('d-m'), $holidays))

{

if($curr=='Sat' || $curr=='Sun')

{

continue;

}

else

{

$days--;

}

}
if($curr == 'Sat' || $curr == 'Sun')

{

$days--;

}

}


It looks correct. What's wrong?

S
Sergej author 7/2/2013



I have made some changes to your code. Please, check the code below.
Let me do an example.
Start date: 2013-01-01

End date: 2013-01-31
Using your code the total days substracted are eight (01-01, 05-01, 12-01, 13-01, 19-01, 20-01, 26-01, 27-01).

$start = new DateTime("2013-01-01");

$end = new DateTime("2013-01-31");

$end->modify('+1 day');

$interval = $end->diff($start);

$days = $interval->days;

$period = new DatePeriod($start, new DateInterval('P1D'), $end);

$holidays = array('01-01','06-01','31-03','01-04','01-05','30-05','22-06', '25-06','05-08','15-08', '08-10', '01-11', '25-12','26-12');
foreach($period as $dt)

{

$curr = $dt->format('D');
//for the updated question

if(in_array($dt->format('d-m'), $holidays))

{

if($curr=='Sat' || $curr=='Sun')

{

continue;

}

else

{

$days--;

}

}
if($curr == 'Sat' || $curr == 'Sun')

{

$days--;

}

}


It looks correct. What's wrong?



Thank you Christian - for the most part code works well - but for example when i put dates like from 22-6 to 25-6 i get 0 working days and it should be 1 - i guess 22.6 gets subtracted twice one time as holiday, one time as saturday....also if i put interval from 22-23/6 it should be 0 working days - i get -1 as a result...but if i put interval 01-02.01 it gets ok result 1 spent day from the vacation days

i'am at a loss completely...thank you as always, Christian

C
cgphp 7/2/2013

Replace this statement:

if(in_array($dt->format('d-m-Y'), $holidays))



with this one:

if(in_array($dt->format('d-m'), $holidays))


Replace this statement:

next();



with this one:

next($period);
S
Sergej author 7/3/2013

thank you Christian - my problem was two part mostly stupidity for not refreshing cache...and also had part of the custom code on list page leftover from previous tryouts - that's why i got the wrong calculations. I have modified the code as per your inputs - thnx again and made slight changes - as per variable holidays like easter monday - would like to automate calculations for them but must see how it is to be done. So far variable holidays must be added manualy in appropriate array. Anyways, if anyone find some use for this - here it is - custom code on list page which calculates spent vaccation days between two dates - excluding weekends and holidays!

$start = new DateTime($data["Od"]);

$end = new DateTime($data["Do"]);

$end->modify('+1 day');

$interval = $end->diff($start);

$days = $interval->days;

$period = new DatePeriod($start, new DateInterval('P1D'), $end);

$period2 = new DatePeriod($start, new DateInterval('P1D'), $end);

$fixedholidays = array('01-01','06-01','01-05','22-06', '25-06','05-08','15-08', '08-10', '01-11', '25-12','26-12');

$variableholidays = array('31-03-2013', '01-04-2013', '30-5-2013');
foreach($period as $dt)

{

$curr = $dt->format('D');

//for the updated question

if(in_array($dt->format('d-m'), $fixedholidays))

{

if($curr=='Sat' || $curr=='Sun')

{

continue;

}

else

{

$days--;

}

}

}

foreach($period as $dt)

{

$curr = $dt->format('D');

//for the updated question

if(in_array($dt->format('d-m-Y'), $variableholidays))

{

if($curr=='Sat' || $curr=='Sun')

{

continue;

}

else

{

$days--;

}

}

}

foreach($period as $dt)

{

$curr = $dt->format('D');

if($curr == 'Sat' || $curr == 'Sun')

{

$days--;

}

}

$value=$days;