双周轮换三种选择,在中午的一个星期五滚动

I need to rotate 3 options based on the current week number; ie:

if($weekNumber == 42 || $weekNumber == 43){
$thisID = 1;    
}               
else if($weekNumber == 44 || $weekNumber == 45){
$thisID = 3;    
}
else if($weekNumber == 46 || $weekNumber == 47){
$thisID = 2;    
}       
else if($weekNumber == 48 || $weekNumber == 49){
$thisID = 1;    
}

I'd obviously prefer this to be less manual! I've considered placing the 3 ID's in an array and selecting based on the number of weeks that have passed since date X but I'm not sure how to implement that.

And additionally... the rollover needs to be at Midday on a Friday. I considered simply incrementing the week number in the above plan too, with additional adjustments for when we're rolling from week 52 to week 1, but there must be a more sensible approach!

Thanks

Why don't you take the rounded half of the week number? Then you only need a catalog holding 26 IDs and you can implement a direct lookup:

$catalog=[1,2,3,4,5,6,3,4,2,5,6,7,4,4,7,9,5,8,8,9,2,4,5,7,6,8,9];
$thisID = $catalog[floor($weekNumber/2)];

This should work:

$options = array(1,2,3);
$offset = 129600;
$twoWeeks = 1209600;
$idx = floor((time()-$offset)/$twoWeeks) % count($options);

$chosenOption = $options[$idx];

Some explanation:

  • 0 Unix time was a Thursday at midnight, so the first Friday at midday was 129600 seconds later.
  • With time()-$offset we shift the time to set Friday at midday as the zero point of our time calculation.
  • Now as Friday at midday is the zero point, we can count the number of full two weeks from the first Friday at midday by simply dividing the passed seconds by 1209600.