如何在PHP中每两周显示不同的图像?

how can I show a different image every 2 weeks?, i have 8 images to show, like this. week 1 image1 week 2 image1 week 3 image2 week 4 image2 ... week 17 image1


<?php
$check=date('W');
    $img1 = array ('1','2','11','12','21','22','31','32','41','42','51','52');
    $img2 = array ('3','4','13','14','23','24','33','32','43','44');
    $img3 = array ('5','6','15','16','25','26','35','36','45','46');
    $img4 = array ('7','8','17','18','27','28','37','38','47','48');
    $img5 = array ('9','10','19','20','29','30','39','40','49','50');
    if (in_array($check,$img1,true)){
    echo "show img1";
    }
    if (in_array($check,$img2,true)){
    echo "show img2";
    }
    if (in_array($check,$img3,true)){
    echo "show img3";
    }
    if (in_arrat($check,$img4,true)){
    echo "show img4";
    }
    if (in_array($check,$img5,true)){
    echo "show img5";
    }
    ?>

it´s correct? can be improved?

save the image paths to a file, every time the function is called check the last modified on the file, if the last modified > 2 weeks, open the file, move the image to the bottom, save the file.

You can use the W parameter from the date function. it gives you the number of the week - using mod (%) you can check if it's divides with the number of the image and show it

Check out the PHP date function. It will provide you a week number which you can use in your logic to find which picture to display.

  • There are 53 weeks (1-53 for W).
  • Images should change every two weeks.
  • There are 8 Images.

Example code:

$week = date('W');
$changes = (int)(($week-1)/2);
$image = $changes % 8 + 1;

printf("Week: %d, Images changed: %d, Current Image: %d
"
       , $week, $changes, $image);