I'm having this problem right now: given a month and a year, I'd need to know how many week days it has (that is, the number of days excluding Saturday and Sunday).
It seems so simple, and yet I'm baffled. Of course I could solve it with a for
loop and check if the day's a Saturday or a Sunday, and if not increment a counter, but this is just plain stupid (and linear time) considering I'm pretty sure I could get away with a couple of divisions or modulos.
Any idea of the algorithm? You have all the power of PHP 4.4.1 at your disposal.
EDIT Here's a working for
loop implementation:
function weekdays_in_month($month, $year)
{
$days_in_month = days_in_month($month); // days_in_month defined somewhere
$first_day = date('w', mktime(0,0,0, $month, 1, $year));
$counter = 0;
for ($i = 0; $i < $days_in_month; $i++)
{
if (($first_day + $i + 1) % 7 >= 2)
$counter++;
}
return $counter;
}
Just check the weekday-ness of the 29th, 30th, and 31st (if these dates exist).
Add 20.
Editing your function: