php – Mini app that shows one unique quote each day. 56 rotating posts rotating for each themed weekday. It works, but I need your advice

Question

Let’s say I have 365 posts published in 2022. I have 56 posts for each weekday (each weekday has a different theme). Normally, if I were to publish them in order, the 1st Monday post would be published on January 3rd and the second Monday post would be published on January 10th. But I want to reuse these posts and for them to be correctly displayed each year, not just in 2022.

So I published batches of posts by weekday. The script below summarizes my data structure:

$sunday_posts = [0, 1, 2... 56];
$monday_posts = [57, 58, 59... 102];
$tuesday_posts = [102, 103, 104... 158];

The values in this array are the post# and day of year they were published (even though 2022 is not over yet).

I then have a conditional script for each weekday:

if(date('D') == "Sun") {
$first_sunday_of_year = gmdate('z', strtotime('first Sunday of january this year'))+1; // sets the anchor, the 1 corrects for 0 start
$sunday_of_year = (((date('z')+1) - $first_sunday_of_year)/7); // returns 19 as of this SE post
return $sunday_list[$sunday_of_year]; // returns correct post day of year
}

This returns the correct day of the year for the post that is needed. For example, every 3rd monday of the year, which is post #59 posted on day 59 of 2022, will show whether it is Monday, 21st of 2023 or Monday, 25th of 2024.

Okay so this works exactly as I hoped for.

However, I just want to know if anyone can suggest if there is a better way to program this? Any general pointers are greatly appreciated from this newbie.

0
Studious Programmer 2 months 2022-05-14T22:50:32-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse