if ($i == $t_days && $day < 6) {
echo "<td colspan=".
7 - $day."></td>\n</tr>\n";
}
Congradulations, you are banging your head against my favorite wall. It's called "operator precedence," and it exists to make your life miserable. Although the concatenation (.) and addition (+) operators have the same precedence, they follow a left associativity--that is, they are evaluated from left to right. So when you say "<td colspan=". 7 - $day, you are telling PHP to concatenate "<td colspan=" and 7 first, then subtract $day from the result. A quote from the aforementioned link:
Parentheses may be used to force precedence, if necessary.
With that in mind, you need to re-write your code as follows:
if ($i == $t_days && $day < 6) {
echo "<td colspan=".
(7 - $day)."></td>\n</tr>\n";
}
This will substract $day from 7, then concatenate the result with the rest of the string. I advise you remember this well; it will save you many nights of sleeplessness.
Hope that helps.