How to calculate the nearest date for a recurring period after a given date (PHP)

I am trying to figure out how to calculate the nearest date after a given date for a repeating period.

For example, if the recurring period is once every two weeks starting January 1, 2016, and the indicated date is January 17, how can I calculate that the next date of the repeating period is January 28?

A recurring period can be any number of days, weeks, months, or years.

Right now, the only solution I can think of is to start from the start date and the loop, adding a recurring period at each iteration until I pass that date, but I wonder if there is a more efficient or elegant solution?

+6
source share
6 answers

You can use DatePeriod to do:

$begin = new DateTime('2016-01-01'); $end = new DateTime('2016-12-31'); $interval = new DateInterval('P14D'); $datePeriod = new DatePeriod($begin, $interval ,$end); $givenDate = new DateTime('2016-01-17'); foreach ($datePeriod as $date) { if ($date < $givenDate) { continue; } echo 'The next recurring period date is ' . $date->format('Ym-d'); break; } 

The conclusion will be:

The next recurring date of the period is 2016-01-29

+4
source

If you are open and not against the database option and mini-cron script, I have a suggestion. Create a table called recurring_track and enter the key value columns:

For instance,

last_recurring_period as key and value: 05-25-2016

Now run the cron script to just update it every time a recurring duration occurs.

Now you just need to query this table to find out what was the last recurring period, and now that there will be the next recurring period with the specified date, you can add and define.

0
source

try it,

  $starting_dat = '2016-01-01'; $recurring_prd = "2 week"; $given_dat = '2016-02-28'; while ($given_dat > $starting_dat) { $next_date=date('Ym-d', strtotime($recurring_prd, strtotime(date($starting_dat)))); $starting_dat = $next_date; } echo date('dm-Y', strtotime($next_date)); 
0
source
  $now = time(); // or your date as well $your_date = strtotime("2010-01-01"); //Get difference in days $datediff = $now - $your_date; // in days say 60 days //use mod with your reoccurring period $remain = $datediff % $recPeriod // her say 2 weeks = 14 days recurring gets you 4 //nearest recured date $recdate = strtotime("-".$remain." day", $now); // 4 days ago Modify similar way for next date too 
0
source

Instead of looping, you can just do some math and use the DateTime class:

 $start = new DateTime("2016-01-01"); $interval = 14; $current = new DateTime("2016-01-17"); // Here we subtract from the interval (14 days) the amount of days remaining // to the next recurring date $daysUntilNext = $interval - ($current->diff($start)->days % $interval); $next = $current->modify("+$daysUntilNext days"); // $next now contains the next recurring date 
0
source

Another approach to this, quite similar to the one from @Matei Mihai, but does not require verification in the final loop. It seems like there should be a more convenient way to add multiple instances of DateInterval to DateTime.

 <?php $start = new DateTime('2016-01-01'); $cutOff = new DateTime('2016-01-17'); $period = new DateInterval('P2W'); // Find out the total number of complete periods between the two dates $distance = $start->diff($cutOff)->days; $periodsBetween = (int) ($distance / $period->d); // Then add on that number of periods + 1 to the original date for ($a=1; $a<=$periodsBetween + 1; $a++) { $start->add($period); } echo $start->format('Ym-d'); // 2016-01-29 
0
source

All Articles