0

I'm trying to loop through and add cells to an HTML table, starting with a start date and adding one day to the start date with each iteration for 4 loops:

$DayCount = 0;

while ($DayCount < 5) {

echo "<td>";
echo date('m/d/Y', strtotime('+$DayCount days', strtotime($UpWeekStart)));
echo "</td>";

$DayCount = $DayCount + 1;

}

Is my syntax wrong? $UpWeekStart is a PHP variable containing user-selected date.

draft
  • 305
  • 2
  • 14
  • 1
    Yes. Escaping a string with single commas tells PHP it shouldn't reinterpret `$words` as variables. You have to use `"`. – Havenard Aug 18 '15 at 22:25

2 Answers2

2

Use " instead of ':

echo date('m/d/Y', strtotime("+$DayCount days", strtotime($UpWeekStart)));

Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. Whereas double quote strings will evaluate the variables in the string. See this answer for more details.

Community
  • 1
  • 1
John Bupit
  • 10,406
  • 8
  • 39
  • 75
1

Remember your loop is starting at zero, and you are stopping it at 4 i.e. < 5, thats 5 iterations. Plus use double quotes around $variables you want expanded as in strtotime("+$DayCount days", strtotime($UpWeekStart)

So try

$DayCount = 0;

while ($DayCount < 4) {

    echo "<td>";
    echo date('m/d/Y', strtotime("+$DayCount days", strtotime($UpWeekStart)));
    echo "</td>";

    $DayCount = $DayCount + 1;

}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149