374c374,380
<     foreach ($weekday as $wd) {
---
>     /* Sort the weekday-array to whatever is set in start_of_week-setting as first day of the week ... */
>     $startofweek=get_settings('start_of_week');
>     for($i=0; $i<count($weekday); $i++) {
> 	$weekday_temp[$i]=$weekday[($i+$startofweek)%7];
>     }
> 	      
>     foreach ($weekday_temp as $wd) {
456c462
<     $pad = intval(date('w', $unixmonth));
---
>     $pad = (intval(date('w', $unixmonth))-$startofweek+7)%7; /* +7 modulo 7 to avoid negative paddings */
477c483,484
<         if (6 == date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear)))
---
>         /* a new row should begin right before day($startofweek) is being echoed (+6 because -1+7=+6 and the whole thing is there to once again avoid negative numbers */
> 	if (($startofweek+6)%7 == date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear)))
481c488,489
<     $pad = 7 - date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear));
---
>     /* find out how much days are left and pad accordingly */
>     $pad = 7 - (date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$startofweek)%7;
