0

PHP Script Snippet (only one holiday):

<?PHP
function calculateBankHolidays($yr) {

    $bankHols = Array();

// New year's:
switch ( date("w", strtotime("01-01-$yr 12:00:00")) ) {
    case 6:
        $bankHols[] = "03-01-$yr";
        break;
    case 0:
        $bankHols[] = "02-01-$yr";
        break;
    default:
        $bankHols[] = "01-01-$yr";
}

// Good friday:
$bankHols[] = date("d-m-y", strtotime( "+".(easter_days($yr) - 2)." days", strtotime("21-03-$yr 12:00:00") ));

// Easter Monday:
$bankHols[] = date("d-m-y", strtotime( "+".(easter_days($yr) + 1)." days", strtotime("21-03-$yr 12:00:00") ));

// May Day:
    if ($yr == 1995) {
        $bankHols[] = "08-05-1995"; // VE day 50th anniversary year exception
    } else {
        switch (date("w", strtotime("01-05-$yr 12:00:00"))) {
            case 0:
                $bankHols[] = "02-05-$yr";
                break;
            case 1:
                $bankHols[] = "01-05-$yr";
                break;
            case 2:
                $bankHols[] = "07-05-$yr";
                break;
            case 3:
                $bankHols[] = "06-05-$yr";
                break;
            case 4:
                $bankHols[] = "05-05-$yr";
                break;
            case 5:
                $bankHols[] = "04-05-$yr";
                break;
            case 6:
                $bankHols[] = "03-05-$yr";
                break;
        }
    }

    return $bankHols;

}

header('Content-Type: application/json');

$bankHolsThisYear = calculateBankHolidays(2017);
echo (json_encode($bankHolsThisYear, JSON_PRETTY_PRINT));
?>

Result:

[
    "02-01-2017",
    "14-04-17",
    "17-04-17",
    "01-05-2017",
    "2017-05-29",
    "2017-08-28",
    "2017-12-25",
    "2017-12-26"
]

Shows current full scripts results

Desired Outcome:

{
        "Holiday Name": {
                "Start Date": ,
                "End Date": ,
                "Holiday type": ,
                "Where it is observed": ,
        },

Questions:

  1. How do I add a "Holiday Name" to the parent of each value?
  2. How do I add "Start Date" to each current value?
2
  • if that particular holiday is just recuring every year, just declare it yourself Commented Dec 5, 2016 at 5:03
  • @Ghost that particular holiday in the script snippet is just one of many, please see the current result, each result is a different holiday. Commented Dec 5, 2016 at 5:10

1 Answer 1

1

You could create an multidimensional associative array of objects and do something like:

    $listOfHolidays=array(
      'halloween'=>array('start'=>'10-31','end'=>'10-31','type'=>'trick or treat','celebratedBy'=>'childhood'),
      'newYear'=>array('start'=>'12-31','end'=>'01-01','type'=>'new year','celebratedBy'=>'everyone'),
);
    echo json_encode($listOfHolidays);

Tested: this is my output:

{
    "halloween":
         {"start":"10-31",
           "end":"10-31",
           "type":"trick ortreat",
           "celebratedBy":"childhood"
         },
    "newYear":
        {"start":"12-31",
         "end":"01-01",
         "type":"new year",
         "celebratedBy":"everyone"
        }
  }

EDIT: as you commented about a switch, i'm not sure i understand the precision but you can easily get the 'holidays' by using the associative keys like so:

$boo=$array['halloween'];

And then get the value from this holiday with:

$boo['type']; //trick ortreat

OR you could alternativly get the value straight from the original array:

echo $array['newYear']['end']; //01-01

further more you can also add a value to the array:

$array['newYear']['bonus']='300$';

Also, just a friendly reminder that you can resotre the aray from jason simply by using the TRUE switch in json_decode like so:

$array=json_decode($json,true);

As for the switch, still i don'T see how you could be using a switch unless you loop through the holidays:

foreach($array as $k=>$v){
  switch($k){
   case 'halloween': echo $v['end']; break; //10-31
   case 'newYear': echo $v['bonus']; break; //300$
   default: echo 'normal work day'; break;
  }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

i have an extra closing bracket in my output since you were missing one in you example. You will have to adjust to your need... or hire someone to code for you...
Thank you for this, it looks like it's almost, not not, what I'm looking for. I'm editing my script snippet with more holidays as I'm not certain how it would work with some other holidays in a case switch I have. +1
Hiys Louis, I don't suppose you've had chance to look at my feedback?
I've been away, i not sure i understand your switch question but you can get the 'holidays' simply by using the associative key. I'll edit the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.