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?

migrated from stackoverflow.com May 17 '16 at 18:55

This question came from our site for professional and enthusiast programmers.

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 <= $midnight; $current = strtotime('+1 week', $current)) {
    $weekMarkers[] = date('m/d/Y', $current);
}

$previous = array_shift($weekMarkers);
foreach ($weekMarkers as $current) {
    echo "<td>$previous - $current</td>\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 <= $midnight; $next = strtotime('+1 week', $next)) {
    echo "<td>$current - ";
    $current = date('m/d/Y', $next);
    echo "$current</td>\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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.