How to print every month in a specified year that contains five full weekends (Fri, Sat, Sun) in PHP

1 Answer

0 votes
// A month has five full weekends when it has 31 days, and the 1st day of the month is a Friday.

// ------------------------------------------------------------
// Month names as a single reusable constant
// ------------------------------------------------------------
const MONTH_NAMES = [
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
];

// ------------------------------------------------------------
// Function: returns true if a month has 5 full weekends
// ------------------------------------------------------------
function hasFiveFullWeekends(int $year, int $month): bool
{
    $firstDay = new DateTimeImmutable("$year-$month-01");

    $daysInMonth = (int)$firstDay->format("t");
    $weekday = $firstDay->format("N"); // 1=Mon ... 5=Fri ... 7=Sun

    return ($daysInMonth === 31 && $weekday == 5); // Friday
}

// ------------------------------------------------------------
// Main program
// ------------------------------------------------------------
$y_value = 2026;

for ($m = 1; $m <= 12; $m++) {
    if (hasFiveFullWeekends($y_value, $m)) {
        echo MONTH_NAMES[$m - 1] . " $y_value has five full weekends.\n";
    }
}


/*
run:

May 2026 has five full weekends.

*/

 



answered May 26 by avibootz
edited May 26 by avibootz

Related questions

...