Counting from 1-19, probably simple but I dont know where to start!

Hi there!

Let’s start by saying that this probably is a really easy thing to do, but I simply don’t know how to effectively search or what this method is called.

I work at a store, and every day my colleagues clean one meter of every aisle in the store, but we keep forgetting which one to clean. I wanted to make a quick and easy script to simply show them the number of the meter to clean, instead of having to print a list every X months. There are 19 meters in each aisle, and 19 always is cleaned on a Friday, meaning that there are 2 days between 19 and the point where the counter should reset to 1.

I could do this by making a database from which a script simply reads the value, but maybe there is an easier way to do this?


Anyone know how I can do this or where I could find more information on how possibly do this?

What happens if there are holidays? Wouldn’t that potentially mess up the start on monday finish on friday stuff?

And this needs to be extended to some arduino stuff with a led on each easle meter number so the one to clean is lit up on the correct day (y)

Like the arduino idea :stuck_out_tongue: but its meant just for a simple webpage with a number on it. With holidays we normally just skip or clean 2 in one day, so that it doesnt go out of sync.

You wont need a database in this case it is just some date calculations. You could calculate the number of days between today’s date and 30-09-2019. and then calculate how many days ago the three week cycle started. Then a loop from 1 to 21 would do, like so:

function getSchedule($currentDate, $cycleDays, $aisles) 
    $out = '';
    // throw an exception if aisles is greater than cycledays
    if($aisles > $cycleDays)
        throw new Exception('CycleDays must be equal or greater than the aisles.');

    // the day that the first schedule starts
    $scheduleStartDate = new DateTime('30-09-2019');

    // calculate how many days we have to go back to start in the three weeks cycle
    $daysBackwards = floor($currentDate->diff($scheduleStartDate)->days % $cycleDays);

    // And than calculate the day to start with in our schedule
    $startDate = $currentDate->sub(new \DateInterval('P' . $daysBackwards . 'D'));

    for ($i = 1; $i <= $cycleDays; $i++) {
        $out .= $startDate->format('D d-m-Y');

        if ($i <= $aisles) {
            $out .= ': aisle ' . $i;
        } else {
            $out .= ': none';
        $out .= '<br>';

        // add one day up to $startDate
        $startDate->add(new \DateInterval('P1D'));
    return $out;

// settings
$cycleDays = 21;
$aisles = 19;

// get schedule on todays date
$currentDate = new DateTime();
echo getSchedule($currentDate, $cycleDays, $aisles);

echo '<br>';

// get schedule on todays date + 21 days
$currentDate->add(new DateInterval('P21D'));
echo getSchedule($currentDate, $cycleDays, $aisles);

See about how the functions work like Datetime::add…

You’re my hero! Thank you so much!

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service