Finding All Weekdays of the Month with PHP

Hello friends. Today, I needed to find all the weekdays of the month, including the current week. You can get the days you want using any code you write. It's very simple. You only need to know the DateTime class.

I only collect weekdays in a series. I don't include weekends. This $lastDay variable Friday After setting it to , at the beginning of each cycle $lastDay I'm updating the variable to 3 days in the future. This way, I find Monday and add each day to the array individually.

modify(('Sunday' == $monday->format('l')) ? 'Monday last week' : 'Monday this week'); // I will set the variable $lastDay in the loop to the previous week's date. So I can update it on a Monday. $lastDay = clone $monday; $lastDay = $lastDay ->modify('-3 day'); // We create an array that specifies 5 weeks. $dates = array( "week1" => array(), "week2" => array(), "week3" => array(), "week4" => array(), "week5" => array() ); //We are starting loop. Here we put our $dates array into foreach. foreach ($dates as $key => $date) { // We use the $lastDay variable to find the first day of the week. $firstDay = clone $lastDay; $firstDay = $firstDay->modify('+3 day'); // After that, we increase the days to finding the remaining 4 days. $secondDay = clone $firstDay; $secondDay = $secondDay->modify('+1 day'); $thirdDay = clone $secondDay; $thirdDay = $thirdDay->modify('+1 day'); $forthDay = clone $thirdDay; $forthDay = $forthDay->modify('+1 day'); $fifthDay = clone $forthDay; $fifthDay = $fifthDay->modify('+1 day'); // We set the $lastDay variable to the 5th day, ie Friday. In this way, we will be able to find Monday at the beginning of the loop. $lastDay = clone $fifthDay; // I am adding dates to the corresponding week in the $date array. I used different indexes here. You can edit it as you like. $dates[$key] = array( $firstDay->format('M d D') => $firstDay->format('Ym-d'), $secondDay->format('M d D') => $secondDay->format('Ym-d'), $thirdDay->format('M d D') => $thirdDay->format('Ym-d'), $forthDay->format('M d D') => $forthDay->format('Ym-d'), $fifthDay->format('M d D') => $fifthDay->format('Ym-d'), ); }
5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments