Why not just take the timestamp of the first day of the month, and subtract 24 hours from it and then use date('d', $timestamp);
? Also, if you have a timestamp in that month, date('t', $timestamp);
will get the number of days in the month for you.
A quick example:
// sets to 3am, first day of month, 3am helps us avoid DST issues with subtracting hours
$monthStart = mktime(3,0,0,date('n'),1,date('Y'));
$thisMonth = date('n', $monthStart);
// rewind to the sunday before- date('w') is the "weekday" 0 based
$date = $monthStart - (date('w',$monthStart) * 60 * 60 * 24);
while ($date<$monthStart || date('n', $date) == $thisMonth)
{
echo "<tr>";
for ($x=0; $x<7; $x++) {
echo "<td";
if (date('n', $date) != $thisMonth) echo " class='notMonth'";
echo ">";
echo date("j", $date);
echo "</td>";
$date += 60*60*24;
}
echo "</tr>\n";
}