我做了一個PHP函數,計算從現在到52周的週數,然後從第1周開始再向左數週。第5周到第52周,然後第1周到第4周共計52周
我想知道是否有更簡單的方法來做到這一點,或者我做出了正確的功能。
這是我的代碼:
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);
輸出:
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,
真棒! thhx爲例。這是一個更好的方法去做 – Julez