What is the best practice for generating an array of weekend dates in a specific month and year using PHP?
To generate an array of weekend dates in a specific month and year using PHP, you can loop through all the days in the month and check if each day falls on a weekend (Saturday or Sunday). If a day is a weekend, add it to the array of weekend dates.
function getWeekendDates($month, $year) {
$weekendDates = [];
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for ($day = 1; $day <= $daysInMonth; $day++) {
$date = date('Y-m-d', strtotime("$year-$month-$day"));
$dayOfWeek = date('N', strtotime($date));
if ($dayOfWeek == 6 || $dayOfWeek == 7) {
$weekendDates[] = $date;
}
}
return $weekendDates;
}
$month = 10;
$year = 2022;
$weekendDates = getWeekendDates($month, $year);
print_r($weekendDates);
Keywords
Related Questions
- What best practices should be followed when designing a homepage for a news system using PHP and MySQL?
- What best practices should be followed when creating regular expressions in PHP for parsing specific patterns within text data?
- What potential issues could arise when upgrading from MySQL 3.x to 4.0.x in a PHP application that involves image uploads?