Question

I made a php function that counts the weeks from now to 52 and then start from week 1 again to the left over weeks.

I wanna know if there is a easier way to do this, or that i made a proper function.

Here is my code:

function weeks($start_week)
                {

                    $start_week     = str_replace("0", "", $start_week); // from week 1 tot 9 remove the zero's.
                    $offset_week    = $start_week - 1; // when the loop reach week 52, start with new weeks minus 1
                    $last_week      = 52; // Last week of the year

                    $aantal_weken   = 52; // # of weeks in a year

                    for($start_week; $start_week <= $aantal_weken; $start_week++)
                    {
                        echo $start_week.", ";

                        if($start_week == $last_week)
                        {   
                            $new_weeks = 1; // when we reach week 52, set a new variable tot first week of the year

                            for($new_weeks; $new_weeks <= $offset_week; $new_weeks++)
                            {
                                echo $new_weeks.", ";                           
                            }
                        }
                    }

                    return $start_week;
                }

                $date = date("W");
                weeks($date);

Output:

4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 1, 2, 3,

Was it helpful?

Solution

Return array with weeks range:

function weeks($start_week) {
    $last_week = date_format(date_create('December 28th'), 'W');
    $range = range(1, $last_week);
    return array_merge(
        array_slice($range, $start_week - 1), 
        array_slice($range, 0, $start_week - 1)
    );
}

demo

OTHER TIPS

Your problem actually isn't related to dates. It is only related to Math. Try the following code:

function weeks($start_week) {
    $arr = array();
    for ($i = 0; $i < 52; $i++) {
        $arr[] = ($i + $start_week - 1) % 52 + 1;
    }
    return implode(', ', $arr);
}
echo weeks(4);
function weeks($start_week)
{
    $start_week     = str_replace("0", "", $start_week); // from week 1 tot 9 remove the zero's.
    $offset_week    = $start_week - 1; // when the loop reach week 52, start with new weeks minus 1
    $last_week      = 52; // Last week of the year
    $aantal_weken   = 52; // # of weeks in a year

    for($start_week; $start_week <= $aantal_weken; $start_week++)
    {
        echo $start_week.", ";

        if($start_week == $last_week)
        {   
            $new_weeks = 1; // when we reach week 52, set a new variable tot first week of the year

            for($new_weeks; $new_weeks <= $offset_week; $new_weeks++)
            {
                echo $new_weeks.", ";                           
            }
        }
    }

    return $start_week;
}

$date = date("W");
weeks($date);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top