Originally posted by Shrike
Hmm tested and yours seems to run a bit (10th of a second?) faster 😃
Not sure how mine goes wrong though...the first code I posted was incorrect, hope your not using that. The second bit works for May, August and October 2004, too lazy to check any more 🙂
Y'know, I could have sworn I was, but it turns out I wasn't. I remember checking that I had use your fixorized version, but ... oh well. Yeah, it is fixed.
I just have this dislike of assuming that a day is 86400 seconds long. Turns out there's no difference in this case, but you can imagine situations where "now+86400 seconds" is still today.
Oh; and as you noticed, every month has exactly four consecutive periods of seven days each, for a total of 28 days (and 20 weekdays). I didn't assume that in my code, but it's true, so I can.
function weekdays_in_month($month, $year)
{
$first = mktime(0,0,1,$month,1,$year);
// The first day of the month is also the first day of the
// remaining days after whole weeks are handled.
list($first_day,$days) = explode(' ',date('w t',$first));
$weekdays = 20;
if($days==28)
return $weekdays; // Only happens most Februarys
$weekdays += $days-29;
// Neither starts on Sunday nor ends on Saturday
if($first_day!=0 && ($first_day+$days-1)%7!=6)
{ // Adjust for weekend days.
$weekdays += ($days<=34-$first_day)-
($days>34-$first_day);
}
return $weekdays;
}