I have this problem right now: given the month and year, I need to know how many days she has (i.e. the number of days, excluding Saturday and Sunday).
It seems so simple, and yet I am puzzled. Of course, I could solve this with a for loop and check if itβs not a day or a Saturday, but if you donβt increase the counter, but thatβs just silly (and linear time), given that Iβm sure I can get away with a few ticks or modules .
Any idea of ββan algorithm? You have all the features of PHP 4.4.1 at your disposal.
EDIT Here's a working implementation of the for loop:
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; }
zneak source share