Is this possible?

I've built a calendar function that mimics the iPhone set up as far as being able to schedule recurring events. I've got "every day, every week, every two weeks, every month and every year."

To do this, I'll just start with the initial date and add whatever timeframe is needed. No biggie.

My client wants an option where they can schedule something on every third Wednesday of the month. Initially, I thought I would just add a "add 21 days" to the scheduling dynamic, but I knew intuitively this wouldn't work because not every month is 30 days. Consequently, I get the "third Wednesday" scenario happening on Wednesday then Friday etc.

My first reaction was to tell the client that the amount of coding to do what they're attempting would be significant just because you're having to anticipate every month and then the way the dates will shift from year to year.

But am I off? Is there a way to schedule "every third Wednesday?" If so, I'd love to get some feedback as to how to write that code. If I am thinking correctly, as far as telling them that they're asking for a volume of code that wouldn't be worth the cost, I'd love to hear some reinforcement there as well. Bottom line: If it's not on the iPhone, chances are there's a reason and in this instance, it's because you can't predict the mathematical value of "every third Wednesday" like you can something that's simply every 21 days.
brucegustPHP DeveloperAsked:
Who is Participating?
Ray PaseurConnect With a Mentor Commented:
You can test your ideas about PHP date/time values on my server here:
käµfm³d 👽Commented:
What does the number of days in the month have to do with the number of days in a week? Adding 21 days to a starting date isn't going to affect how many Wednesdays fall within that 21-day range. Adding 21 should be fine (for every 3rd Wednesday).

If you were to say that you wanted to schedule meetings the 3rd of every month, THEN you could not simply say "Jan 3rd + 30 days". See the difference?
brucegustPHP DeveloperAuthor Commented:
Right now, based on the way that I've coded things, if I say I want an even every second Tuesday, I'll schedule it April 8th and hit "submit." When I look at May, the 8th is on a Thursday as opposed to a Tuesday. So I've just frustrated my user because I'm adding 30 days instead of 4 weeks.

I don't have my code in front of me and I put this together a while ago so I'm sorry I can't be more specific with what I've done, but the bottom line seems to be that instead of adding 30 days to a date, I have code available to me that allows me to do something similar to:

Jan 3rd + 4 weeks, rather than Jan 3rd + 30 days..

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Are you trying to do this in PHP or Js?

In php you could do something like this

$get_by_dayname = strtotime('2014-03-01 next wednesday');
echo date("d-m-y",$get_by_dayname);
brucegustPHP DeveloperAuthor Commented:
I'm doing this in PHP.

So, if my user wants to schedule an event every third Wednesday, my code needs to be something like:

$everythirdWednesday  = strtotime('$this_Wednesday'+ three weeks);

That puts my event on the third Wednesday of every month. As opposed to this:

$everythreeweeks = strtotime('$this_Wednesday'+21days);

Use the code above to find the first Wednesday then add 14 days or 2 weeks to it
Obviously you need to inject the correct month into the date, March is just an example.
Ray PaseurConnect With a Mentor Commented:
Please see this article that answers the instant question and many others.

Every 3rd Wednesday is easily found by adding 21 days to the initial Wednesday.  The 3rd Wednesday of a month is easily found with something like March 1 + 3 Wednesday
käµfm³d 👽Connect With a Mentor Commented:
...if I say I want an even every second Tuesday, I'll schedule it April 8th and hit "submit." When I look at May, the 8th is on a Thursday as opposed to a Tuesday.
That's because you're mixing up your requirement. Your requirement is not "what day of the week is the 8th of May"; your requirement is "which Tuesday is this (first, second, etc.)".
GaryConnect With a Mentor Commented:
My example above would break in October this year, so here's a better way

echo date("d-m-y",strtotime('first wednesday of october 2014 2 weeks'));

Open in new window

Don't think you can get much simpler and easier to understand than that!
Ok yours works too and shorter.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.