0

I am trying to display a number every day in a loop. After the last element is reached it needs to get to the first element again. This needs to happen daily. I have overworked my brains out but did not managed to solve it. Function needs to return current number by day/hour/minute, like . This is what I tried till now.

<?php
function recursive_daily_deals($i = 1) {
    $current_date = strtotime(date('d-m-Y h:i:s'));
    $dbs_date_1 = strtotime('29-06-2017 8:20:16');
    $current_hour = date('h');
    var_dump($current_hour);
    $products = [
        451,
        455,
        453
    ];
    if ($i < count($products)) {
        return recursive_daily_deals($i+1);
    }
}
?>

EXPECTED output

>     First day - June 29 2017
>       It will appear 451
>     Second day - June 30 2017
>       It will appear 455
>     3rd day - July 1st 2017
>       It will appear 453
>     4th Day - July 2nd 2017
>        It will appear 453 
>     And start over
Adrian
  • 2,273
  • 4
  • 38
  • 78
  • @mickmackusa Because I use PHP 7 and I need this to do in php. – Adrian Jun 30 '17 at 06:10
  • What output do you currently get – Rotimi Jun 30 '17 at 06:11
  • Then why have you tagged it with `5.6`? Why should 4th day say `453`? What are you doing with `$current_hour`? There are major differences between what you've tried and your expected result. – mickmackusa Jun 30 '17 at 06:12
  • @Akintunde, none because not sure how to solve it. – Adrian Jun 30 '17 at 06:13
  • @mickmackusa: Removed Tag 5.6, was thinking is same thing. This is what I am working my brain, current code is not doing expected output. – Adrian Jun 30 '17 at 06:14
  • Do you want a collection of dates, that increase by one day until an end date? Like this: https://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array ? – mickmackusa Jun 30 '17 at 06:14
  • I need those number to appear at an interval of 24 hours, no end date, infinite date. And array is dynamic could have unlimited number of elements or lets say 10 elements. – Adrian Jun 30 '17 at 06:16
  • @mickmackusa: Exactly, but forever, like for example if there are 3 products in the array, in the 4th day to re-start process. Something Like Daily Deals. – Adrian Jun 30 '17 at 06:19

2 Answers2

2

First you need to know how many days have been since the starting day. To do that you just need to sub the starting timestamp from the actual timestamp :

$dbs_date_1 = strtotime('29-06-2017 8:20:16');
$actualTimestamp = time();
$elapsedSec = $dbs_date_1 - $actualTimestamp;

// we need to get days from seconds
$elapsedDays = $elapsedSec / (3600*24);
$elapsedDays = floor($elapsedDays);

So when you have how many days have been since the starting day. We use floor() instead of round() because if the script runs after the half of the day it will return the number of days +1.

With this number of days we can have the number of cycles already done by dividing the number of elapsed days by the number of items in our array :

$nbItems = count($products);
$cycleCount = $elapsedDays / $nbItems;
$finishedCycles = floor($cycleCount);

We store the number of finished cycles by flooring the number of cycles. Then we just have to sub the days it took to complete those cycles from the elapsed days and we will get the position of the index.

 $completeDays = $finishedCycles * $nbItems;
 $actualPosition = $elapsedDays - $completeDays;
 return $products[$actualPosition];
Onoulade
  • 138
  • 10
1

While this is a simplified version of the code originally posted, I believe it contains the kind of logic that the OP seeks, as follows:

   <?php
   $products = [
        451,
        455,
        453
    ]; 

    $modfactor = count($products);
    $days = null;

    $weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];

    for ($i=0, $max = 7; $i < $max; $i++) {
          $days[$i] = $i % $modfactor;        
    }


    foreach ($weekdays as $dex => $wday) {
           echo "$wday, ";
           echo $products[ $days[$dex] ], "\n";
    }

See demo

Update: See demo here which makes use of array_map() and gets the current product ID, too.

While the loop is ever present, it is not infinite. If the number of products changes, then the modfactor changes, too. What stays constant are the days of the week. What makes the code work is taking advantage of a modulo operation.

slevy1
  • 3,797
  • 2
  • 27
  • 33
  • So in order to oupt the current ID Will need to do: `echo $products[$days[0]];` ? – Adrian Jun 30 '17 at 08:44
  • The current ID is stored in the array $days which is mapped to $weekdays. So, if it is currently Friday, then outputting the current ID involves echo $products[$days[4]] -- see here: https://3v4l.org/isNAG – slevy1 Jun 30 '17 at 10:15
  • Thank you. Also in order to make the weekdays start dinamic, created the array like this: ` $timestamp = strtotime($weekday); // Saturday $days = array(); for ($i = 0; $i < 7; $i++) { $weekdays[] = strftime('%A', $timestamp); $timestamp = strtotime('+1 day', $timestamp); }` So this would have a precise time based on the weekday via this method, first method is VIA the exact time (hour / min / sec) starting. – Adrian Jun 30 '17 at 12:59