Printing the start and end dates of the most recent eight weeks I have a table with columns dated by week that changes dynamically each day. Currently, I am using the following php code to get the proper dates for the past eight weeks:

$currentDate = date('Y-m-d');
$weekOneStart = strtotime("-56 day", strtotime($currentDate));
$weekOneStart = date("m/d/Y", $weekOneStart);
$weekOneEnd = strtotime("-49 day", strtotime($currentDate));
$weekOneEnd = date('m/d/Y', $weekOneEnd);
$weekTwoStart = strtotime("-49 day", strtotime($currentDate));
$weekTwoStart = date("m/d/Y", $weekTwoStart);
$weekTwoEnd = strtotime("-41 day", strtotime($currentDate));
$weekTwoEnd = date('m/d/Y', $weekTwoEnd);
$weekThreeStart = strtotime("-41 day", strtotime($currentDate));
$weekThreeStart = date("m/d/Y", $weekThreeStart);
$weekThreeEnd = strtotime("-34 day", strtotime($currentDate));
$weekThreeEnd = date('m/d/Y', $weekThreeEnd);
$weekFourStart = strtotime("-34 day", strtotime($currentDate));
$weekFourStart = date("m/d/Y", $weekFourStart);
$weekFourEnd = strtotime("-27 day", strtotime($currentDate));
$weekFourEnd = date('m/d/Y', $weekFourEnd);
$weekFiveStart = strtotime("-27 day", strtotime($currentDate));
$weekFiveStart = date("m/d/Y", $weekFiveStart);
$weekFiveEnd = strtotime("-20 day", strtotime($currentDate));
$weekFiveEnd = date('m/d/Y', $weekFiveEnd);
$weekSixStart = strtotime("-20 day", strtotime($currentDate));
$weekSixStart = date("m/d/Y", $weekSixStart);
$weekSixEnd = strtotime("-13 day", strtotime($currentDate));
$weekSixEnd = date('m/d/Y', $weekSixEnd);
$weekSevenStart = strtotime("-13 day", strtotime($currentDate));
$weekSevenStart = date("m/d/Y", $weekSevenStart);
$weekSevenEnd = strtotime("-6 day", strtotime($currentDate));
$weekSevenEnd = date('m/d/Y', $weekSevenEnd);
$weekEightStart = strtotime("-6 day", strtotime($currentDate));
$weekEightStart = date("m/d/Y", $weekEightStart);
$weekEightEnd = $currentDate;


and echoing the results in my table like so:

&lt;td&gt;$weekOneStart - $weekOneEnd&lt;/td&gt;
&lt;td&gt;$weekTwoStart - $weekTwoEnd&lt;/td&gt;
&lt;td&gt;$weekThreeStart - $weekThreeEnd&lt;/td&gt;
&lt;td&gt;$weekFourStart - $weekFourEnd&lt;/td&gt;
&lt;td&gt;$weekFiveStart - $weekFiveEnd&lt;/td&gt;
&lt;td&gt;$weekSixStart - $weekSixEnd&lt;/td&gt;
&lt;td&gt;$weekSevenStart - $weekSevenEnd&lt;/td&gt;
&lt;td&gt;$weekEightStart - $weekEightEnd&lt;/td&gt;


This works find and gives me the info I need, but I feel like there must be a simpler way to do this. I tried (with my variables, just posting the generic code here since I already deleted my version):


$date = '2009-12-06';
// End date
$end_date = '2020-12-31';

while (strtotime($date) &lt;= strtotime($end_date)) {
            echo "$date\n";
            $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
 }



But this didn't work for me. Any other suggestions for cleaning/simplifying my code?
Don't Repeat Yourself


$currentDate = date('Y-m-d');
$weekOneStart = strtotime("-56 day", strtotime($currentDate));



You can simplify this by saying something like 

$midnight = strtotime(date('Y-m-d'));
$weekOneStart = strtotime("-56 day", $midnight);


Then you don't have to keep taking strtotime of the same value.  


$weekOneEnd = strtotime("-49 day", strtotime($currentDate));
$weekOneEnd = date('m/d/Y', $weekOneEnd);
$weekTwoStart = strtotime("-49 day", strtotime($currentDate));
$weekTwoStart = date("m/d/Y", $weekTwoStart);



Similarly, consider reducing this to 

$weekOneEnd = strtotime("-49 day", $midnight);
$weekOneEnd = date('m/d/Y', $weekOneEnd);
$weekTwoStart = $weekOneEnd;


Now you only calculate that value once.  

As a for loop

$weekMarkers = [];
$midnight = strtotime(date('Y-m-d'));

for ($current = strtotime('-8 weeks', $midnight); $current &lt;= $midnight; $current = strtotime('+1 week', $current)) {
    $weekMarkers[] = date('m/d/Y', $current);
}

$previous = array_shift($weekMarkers);
foreach ($weekMarkers as $current) {
    echo "&lt;td&gt;$previous - $current&lt;/td&gt;\n";
    $previous = $current;
}


This is actually as two for loops.  You could mix them if you wanted, although many would argue that you are mixing calculation and display in that case.  

$midnight = strtotime(date('Y-m-d'));

$current = date('m/d/Y', strtotime('-8 weeks', $midnight));
for ($next = strtotime('-7 weeks', $midnight); $next &lt;= $midnight; $next = strtotime('+1 week', $next)) {
    echo "&lt;td&gt;$current - ";
    $current = date('m/d/Y', $next);
    echo "$current&lt;/td&gt;\n";
}


This would work, although the double use of $current is a bit odd.  

I leave it up to you whether either of these is easier than your original code.