Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

I want to add a repeating event for a period defined by a start date and an ending date. How?

So I've got this much figured out:

Say my user has a calendar event that they want to schedule to occur every two weeks for the next two months. To figure out the first date after their initial start date, I've got this:

$date = date_create('2013-07-16');
date_add($date, date_interval_create_from_date_string('14 days'));
echo date_format($date, 'Y-m-d'); //the next date in the sequence

I've yet to create a loop that would insert the data, but I'm stuck as far as how I would create a script that first calculates the number of times I would loop through the data.

For example, I'm looking at this:

// Show event dates for next 2 years
// one year = ~52 weeks
// 52 weeks / 4 weeks intervals = 13 months ;)
// So, two years = 26 intervals with 4 weeks
//
for($i = 1; $i <= 26; $i++)
{
    $weekOffset = $i * 4;
    $nextDate = strtotime("{$startDate} +{$weekOffset} weeks");
    echo date('Y-m-d l', $nextDate) . PHP_EOL.'<BR>';
}

But how do I do the computations that produce the $i variable based on the dates my user inputs as the start and end dates?

Thoughts?
SOLUTION
Avatar of zappafan2k2
zappafan2k2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Yes!

What I need to know is how to calculate the $i variable based on the user having entered a start and an ending date.

In your example, you use 60 days. How would I calculate the difference between my user's start and date so the result is a figure that I can plug into your code?

Secondly, what is $offset? I don't want to just copy and paste your solution, I want to understand why what you're doing works.

Thanks for your time!
Please consider taking a little time to read and digest this article.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

The term "month" is ambiguous.  Seconds, Minutes, Hours, Days, Weeks are all unit-record with predictable meanings.  Months and Years (and anything else built on these "shifting sands" terms) are not always the same.  What is the number of days or weeks in the month of February?  It depends on the year!  What day is a month before March 31?  Or for that matter what day is a month after March 31?  Not only does it depend on the year, but it also depends on the definition you attach to "month" and your definition may not make complete sense to the clients who use your web site.

The reason that @zappafan2k2 has a potentially good solution is because it makes reference to weeks, not months.  I do not have a teaching example, but I can envision a UX that allows the client to check a box for every repeating date, and to check a separate box to show the next three months (or the next calendar year) with checkboxes for each repeating date.

Best to all, ~Ray
Ray, always a treat hearing from you, sir!

I believe I'm poised on the threshold of great things, it's just the "range" that's still killing me.

Every example I've seen thus far is very intuitive as far as mapping out increments that match my user's preference.

And I totally understand what you and @zappafan are driving at, but it's figuring out the value of $i.

If my user's start date is July 16th, 2013 and his end date is September 30th, 2013 and he wants to schedule a recurring meeting that happens every second Tuesday between those two dates, how do I figure out the value of $i?

That's my hurdle right now.

Nice article, by the way!
Thanks.  I will look at this tomorrow.  "Every second Tuesday" may be reduced to a schedule.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial