Jump to content

Recommended Posts

I have a function here from https://toroguapo.com/gf-hero/articles/calculating-holidays-for-a-given-year-in-php

function get_holidays(int $year): array {
    $holidays = [
        // Fixed holidays

        'new_years_day' => new DateTime("$year-01-01"),
        'juneteenth' => new DateTime("$year-06-19"),
        'independence_day' => new DateTime("$year-07-04"),
        'veterans_day' => new DateTime("$year-11-11"),
        'christmas' => new DateTime("$year-12-25"),
         'Valentines Day' => new DateTime("$year-02-14"),

        // Variable holidays

        'martin_luther_king_day' => new DateTime(
            "third Monday of January $year"
        ),
        'washington_day' => new DateTime(
            "third Monday of February $year"
        ),
        'memorial_day' => new DateTime(
            "last Monday of May $year"
        ),
        'labor_day' => new DateTime(
            "first Monday of September $year"
        ),
        'columbus_day' => new DateTime(
            "second Monday of October $year"
        ),
        'thanksgiving_day' => new DateTime(
            "fourth Thursday of November $year"
        ),
    ];

    // Inauguration Day, every 4th year

    if ($year % 4 === 0) {
        $inauguration_day = new DateTime("$year-01-20");

        if ($inauguration_day->format('l') === 'Sunday') {
            $inauguration_day->modify('+1 day');
        }

        $holidays['inauguration_day'] = $inauguration_day;
    }

    // Easter

    $easter = new DateTime("$year-03-21");
    $easter->modify('+' . easter_days($year) . ' days');
    $holidays['easter'] = $easter;

    return $holidays;
}

function list_holidays(DateTimeInterface $date): bool {
    $year = (int) $date->format('Y');

    $date_formatted = $date->format('Y-m-d');
    foreach (get_holidays($year) as $holiday) {
        $holiday_formatted = $holiday->format('Y-m-d');
        echo $holiday_formatted;
    }
    }

 

Basically what im trying to do is run a foreach to loop and for every holiday echo out "Holiday Name is Date" I know im close but cant quite figure it out, anyone willing to help me get this working? Id be eternally grateful.

The loop iterates over the holidays array, but it does not access the keys (holiday names) associated with the DateTime objects.

Modify the foreach loop in the list_holidays function to include both the holiday names and their corresponding dates.

Edited by Moorcam
2 hours ago, Moorcam said:

The loop iterates over the holidays array, but it does not access the keys (holiday names) associated with the DateTime objects.

Modify the foreach loop in the list_holidays function to include both the holiday names and their corresponding dates.

im only a novice and im not so great with foreach loops, im not quite sure what I need to do im afraid

6 hours ago, NoahJay said:

im only a novice and im not so great with foreach loops, im not quite sure what I need to do im afraid

That's ok. We all learn as we go along :)

As Barand said above, the syntax you need is in the  foreach loop.

Try this. Replace your function list_holidays with this:

function list_holidays(DateTimeInterface $date): bool {
    $year = (int) $date->format('Y');

    foreach (get_holidays($year) as $holiday_name => $holiday_date) {
        $holiday_formatted = $holiday_date->format('Y-m-d');
        echo "$holiday_name is $holiday_formatted\n";
    }
    return true; // Added return statement for consistency
}

Look at the code above, get familiar with what it does. That's how you learn. We are now using the foreach loop to retrieve the holiday name and holiday date. This should now output in the way you want. See how it goes :)

Doing a quick Google (we all know Dr. Google knows everything, right?) It explains what a foreach loop is:

Quote

A foreach loop is a type of control flow statement that is used to iterate through a collection, such as an array, list, or other iterable structure, and execute a block of code for each element in the collection. It simplifies iterating over elements without needing to manage index variables explicitly.

 

Edited by Moorcam

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.