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:
<td>$weekOneStart - $weekOneEnd</td>
<td>$weekTwoStart - $weekTwoEnd</td>
<td>$weekThreeStart - $weekThreeEnd</td>
<td>$weekFourStart - $weekFourEnd</td>
<td>$weekFiveStart - $weekFiveEnd</td>
<td>$weekSixStart - $weekSixEnd</td>
<td>$weekSevenStart - $weekSevenEnd</td>
<td>$weekEightStart - $weekEightEnd</td>
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) <= 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?