gpt4 book ai didi

php - 获取过去 X 周的周数

转载 作者:行者123 更新时间:2023-12-02 06:18:29 25 4
gpt4 key购买 nike

我有一个脚本可以像这样构建过去 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++;
}

关于php - 获取过去 X 周的周数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18695747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com