Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Automation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

5-Line PHP Script to Rotate Content Daily

Note: The code and the how-to were rewritten since publication. Instead of day number with year number appended, the number to divide is now year number with day number appended.

When you have content — quotes, ads, images, … — to show on rotation, a different one each day, consider using this 5-line PHP script.

To set it up, put the rotation content files into a special directory. Then put the PHP code into your page.

Every day, one of the files in that special directory will be published where you put the PHP code.

The same content will be published all day. Then the next day, the content in a different file will be published. When as many days have passed as there are files, the rotation begins anew.

Once it's set up, adding and removing content is as simple as adding and removing files in the special directory. The PHP code on the pages doesn't need to be changed.

When it comes to publishing different content every day, on rotation, this may be the easiest it's ever been.

Detailed Instructions

The content files —

Create a directory for the rotation content. For the examples, I'll assume /rotate is the directory name.

Put your individual content files in the /rotate directory — an individual file for each day's content.

The file names in the /rotate directory need to end with .php so the software will recognize them for what they are. There are no other file name restrictions.

The file content isn't required to contain PHP code. That's optional.

But the file content does need appropriate HTML markup for the content you are publishing. If the content includes an image, it needs an img tag. If a form, it needs form markup. And so forth. Use whatever HTML markup is required to style the content the way you want it published.

There must be at least one file in the /rotate directory. (An empty directory will result in a "division by 0" error.) The maximum number of files is the year number. This year, the maximum is 2024. Next year, the maximum number of files in the /rotate directory will be 2025.

(If you need more than 2024 content rotation files in the /rotate directory, contact me about modifying the PHP code so you can have the number of files you need.)

There is nothing in the content or in the file name that indicates what day it is to be published.

One file will be selected every day for the entire day. Then another one the next day. If you're interested on how its determined, see How the Calculations Are Done.

Publishing the content —

To publish the content, insert these 5 lines of PHP code into your web page where the day's content will be published. (I'm assuming /rotate is the directory with the rotation content.)

<?php
$Directory = '/rotate';
$FileList = glob("{$_SERVER['DOCUMENT_ROOT']}$Directory/*.php");
include($FileList[(intval(date('Yz'))%count($FileList))]);
?>

If /rotate is not the directory with your rotation content, change the PHP code accordingly.

Which file to publish is determined by the PHP code from elements of the current date.

During the entire day, the PHP code will publish the content of one of the files. On the next day, the content of the next file will be published. When all .php files have had their day to be published, the rotation begins anew.

If files are added or removed from the /rotate directory, then the rotation sequence may be interrupted and the start point reset. It depends on the file name and whether or not the server sorts file names when PHP software asks for a list.

If a new start point is needed, the reset is automatic. There's nothing you have to do.

If the start point is automatically reset, then every file will be shown once per day from that point; and restarted at that point when all files have had their publish day.

How the Calculations Are Done

Understanding this section is optional. Read only if you're interested.

To determine which file contains today's content, a number is constructed from elements of the current date. That constructed number is divided by the number of PHP files in the rotation directory. The remainder after the division (which may be 0) is used to determine today's content.

Example:

Let's first construct the number from the current date.

The construction of the number is the current 4-digit year with the current day of the year appended.

The day of the year is the number 0 on the first day of January. The number increments every day until the last day of December (which will be number 364 or 365, depending on whether or not it's a leap year).

Today being May 2, 2024, the day of the year number is 122. With the year 2024 prepended, the constructed number becomes 2024122.

(Every day, the constructed number will be different. The number is incremented by 1 every day until the last day of the year. At the beginning of next year, a new sequence starts with 20250.)

To complete the calculation, let's assume the number of files in the rotate directory is 29.

The calculation determines which file contains today's content. The constructed number (2024122) is divided by the number of files (29) to determine the remainder. The remainder of 2024122÷29 is 9. The remainder, the number 9, determines the file for today's content.

(This article first appeared in Possibilities ezine.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC