Saturday, January 5, 2008

Useful way to calculate week of the month

I was looking at the date function in php to see if there was a method for calculating which week of the month a given date was. Example March 1st 2008 would be the first Saturday of March. Unfortunately there is no function that returns this information. However, I did find a reference in the discussion that followed the date function. Someone asked the exact same question. The following code is almost identical to what they posted.

$date = getDate(mktime(0,0,0,$month,$day,$year));
$days = $date['mday']-1;
$day = intval($days/7)+1;

No comments: