What are the potential drawbacks of using a simple FOR loop to count weekdays in PHP?

Using a simple FOR loop to count weekdays in PHP may not account for weekends, resulting in an inaccurate count. To solve this issue, we can use the DateTime class in PHP to accurately determine weekdays and exclude weekends from the count.

// Initialize variables
$startDate = new DateTime('2022-01-01');
$endDate = new DateTime('2022-01-31');
$weekdays = 0;

// Loop through each day and count weekdays
while ($startDate <= $endDate) {
    if ($startDate->format('N') < 6) { // Check if day is a weekday (1-5)
        $weekdays++;
    }
    $startDate->modify('+1 day'); // Move to the next day
}

echo "Total weekdays in January 2022: " . $weekdays;