我有一个脚本,可以生成过去12周的周数数组,如下所示:

$week_numbers = range(date('W'), date('W')-11, -1);

但是,如果当前星期数为1,则将返回如下数组:
Array
(
    [0] => 1
    [1] => 0
    [2] => -1
    [3] => -2
    [4] => -3
    [5] => -4
    [6] => -5
    [7] => -6
    [8] => -7
    [9] => -8
    [10] => -9
    [11] => -10
)

但是我需要这个数组看起来像这样:
Array
(
    [0] => 1
    [1] => 52
    [2] => 51
    [3] => 50
    [4] => 49
    [5] => 48
    [6] => 47
    [7] => 46
    [8] => 45
    [9] => 44
    [10] => 43
    [11] => 42
)

谁能看到一个简单的解决方案?

我已经考虑过要做这样的事情(未经测试):
$current_week_number = date('W');

if($current_week_number<12){
    // Calculate the first range of week numbers (for current year)
    $this_year_week_numbers = range(date('W'), 1, -1);
    // Calculate the next range of week numbers (for last year)
    $last_year_week_numbers = range(52, 52-(11-$current_week_number), -1);
    // Combine the two arrays to return the week numbers for the last 12 weeks
    $week_numbers = array_merge($this_year_week_numbers,$last_year_week_numbers);
}else{
    // Calculate the week numbers the easy way
    $week_numbers = range(date('W'), date('W')-11, -1);
}

最佳答案

一个想法

$i = 1;
while ($i <= 11) {

echo date('W', strtotime("-$i week")); //1 week ago

$i++;
}

10-05 19:29