NoahJay Posted 10 hours ago Share Posted 10 hours ago 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. Quote Link to comment https://forums.phpfreaks.com/topic/326563-help-with-a-function-dealing-with-holidays/ Share on other sites More sharing options...
Moorcam Posted 10 hours ago Share Posted 10 hours ago (edited) 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 10 hours ago by Moorcam Quote Link to comment https://forums.phpfreaks.com/topic/326563-help-with-a-function-dealing-with-holidays/#findComment-1648123 Share on other sites More sharing options...
NoahJay Posted 7 hours ago Author Share Posted 7 hours ago 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 Quote Link to comment https://forums.phpfreaks.com/topic/326563-help-with-a-function-dealing-with-holidays/#findComment-1648133 Share on other sites More sharing options...
Barand Posted 6 hours ago Share Posted 6 hours ago The syntax you want is foreach (get_holidays($year) as $holiday_name => $holiday) { . . . } Quote Link to comment https://forums.phpfreaks.com/topic/326563-help-with-a-function-dealing-with-holidays/#findComment-1648137 Share on other sites More sharing options...
Moorcam Posted 1 hour ago Share Posted 1 hour ago (edited) 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 49 minutes ago by Moorcam Quote Link to comment https://forums.phpfreaks.com/topic/326563-help-with-a-function-dealing-with-holidays/#findComment-1648153 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.