Well, this is gonna be in pseudo code but I'll try and stick to the variables that you put up.
month_start = ((7 - day % 7) + day_of_week + 1) % 7
echo <tr>
for(i = 0; i < month_start; i++)
{
echo <td>blank entry</td>
}
for(i = 1; i <= 7 - month_start; i++)
{
echo <td>ith entry</td>
}
echo </tr>
tracker = 0 // some variable that I created to keep track of the cyclic day of the week (just a counter really)
for(i = 7 - month_start + 1; i <= days; i++)
{
if(tracker == 0)
{
echo <tr>
}
echo <td>ith entry</td>
tracker++
if(tracker == 7)
{
echo </tr>
tracker = 0
}
}
for(i = 0; i < 7 - (days + month_start) % 7; i++)
{
echo <td>blank entry</td>
}
if((days + month_start) % 7 != 0)
{
echo </tr>
} I got no clue if it works though, that's all brain mathematics hehe