PHP - find the next future recurring date?

I have a database of events that are "static" (on a specific day) and "repeating" (starting on a specific day, but are set to "repeating" every week or every other week). I understand that there should be intervals of 7 and 14, but I do not know how to get to this point to find the date> today and spit it out.

therefore an example;

I want to find the next upcoming recurring events and spit out their respective dates. Side note: the data I'm stuck in is in the lines (I know, I know) of Ymd, so 20150821 will be on August 21, 2015.

if today was August 21, 2015, and there is a recurring event for โ€œevery other Fridayโ€ starting August 7, it would be +14, which will deliver you today, Friday, August 21.

But say that one of them "every Wednesday", starting from August 19, I want to get the date on Wednesday, August 26, and spit it out.

In the future, this will require endless dates, since start dates do not change.

So, by running the script on January 1, 2016, I will need to know that the next "Every Wednesday" was January 6, 2016.

pseudo code:

if(start_date < today_date) { // start date is in the past add recurring_inverval to start_date until result >= today_date echo result } elseif(start_date > today_date { // start date is in the future echo start_date } 

this is the add up to x in which I got lost. Not sure how to do this in an if statement.

also not sure if this is the best way to do this. I know that PHP can also execute complex strings and convert them to a date. like "Next Saturday"

0
source share
2 answers

I know that you answered this yourself, but another option is to simply use the module (remainder after deletion) of the current date minus the start date to calculate the next date. Here is a quick script to do just that:

 <?php function nextDate($start_date,$interval_days,$output_format){ $start = strtotime($start_date); $end = strtotime(date('Ym-d')); $days_ago = ($end - $start) / 24 / 60 / 60; if($days_ago < 0)return date($output_format,$start); $remainder_days = $days_ago % $interval_days; if($remainder_days > 0){ $new_date_string = "+" . ($interval_days - $remainder_days) . " days"; } else { $new_date_string = "today"; } return date($output_format,strtotime($new_date_string)); } echo nextDate('20151210',14,'Ymd') . "<br />"; echo nextDate('20150808',14,'Ymd') . "<br />"; ?> 

You also do not want to return the early date if the โ€œstart dateโ€ is in the distant future. Updated code to prevent this.

+1
source

I decided myself:

 $begin = new DateTime($start_date); // start searching on the start date of the event $end = new DateTime(date('Ymd')); // end on today date $end = $end->modify('+1 month'); // extend search to the next month $interval = new DateInterval('P'. $freq_math .'D'); // intervals of Plus X Days - uses frequency math of 7 or 14 for every or every other $daterange = new DatePeriod($begin, $interval, $end); foreach($daterange as $date) { if($date->format('Ymd') >= date('Ymd')) { $next_recurring_date = $date->format('Ymd'); break; } } echo $next_recurring_date; 
0
source

All Articles