Pascal Klein
Pascal Klein

Reputation: 24903

PHP: Loop through all weeks in a date range (last 2 years)?

I like to iterate through all weeks in a date range that spawns 2 years. Starting at the current week number two years ago, until the current week number this year.

The problem is that a year can either have 52 weeks or 53 weeks, for example:

So this is currently my php code:

# start with the current week 2 years ago
$year = date("Y") - 2;
$week = (int) date("W");    // (int) removes leading zero for weeks < 10

$endYear = date("Y");
$endWeek = (int) date("W");

# iterate through all weeks until now
do {
    echo $week. " of ". $year;

    $week++;
    if ($week > 52) { // or greater 53 ?????????????
        $year ++;
        $week = 1;
    }
}
while ($year < $endYear || $week < $endWeek);

Upvotes: 0

Views: 5783

Answers (3)

Pascal Klein
Pascal Klein

Reputation: 24903

With the help of Ross Wilson and John Conde I found a solution:

$now = new DateTime;

$from = (new DateTime)->setISODate($now->format('Y') - 2, $now->format('W'));
$from->modify("thursday this week"); // see ISO 8601 

while($from < $now) {
    echo $from->format('W Y').'<br />';

    $from->modify("+1 week");
}

It is important so set the week day to thursday, because according to ISO 8601, the week "belongs" to the year which still contains the thursday.

Upvotes: 3

Rwd
Rwd

Reputation: 35200

You could use DateTime and setISODate for something like this:

$now = new DateTime;

$from = (new DateTime)->setISODate($now->format('Y') - 2, $now->format('W'));

while($from < $now) {

    echo $from->format('W Y').'<br />';

    $from->modify("1 week");
}

Hope this helps!

Upvotes: 1

John Conde
John Conde

Reputation: 219874

Instead of trying to keep track of the bounds, let PHP do it for you.

$start    = new DateTime('-2 years');
$end      = new DateTime();
$interval = new DateInterval('P1W');
$period   = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
    echo $date->format('W') . " of ". $date->format('Y') . "\n";
}

Demo

Upvotes: 5

Related Questions