Solved

Is this possible?

Posted on 2014-03-26
10
112 Views
Last Modified: 2014-03-31
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.
0
Comment
Question by:brucegust
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39956208
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?
0
 

Author Comment

by:brucegust
ID: 39956262
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..

Correct?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39956302
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);
0
 

Author Comment

by:brucegust
ID: 39956359
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);

Yes?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39956367
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.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39956394
Please see this article that answers the instant question and many others.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

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
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 125 total points
ID: 39956520
...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.)".
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 125 total points
ID: 39957124
My example above would break in October this year, so here's a better way

<?php
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!
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39957216
You can test your ideas about PHP date/time values on my server here:
http://iconoun.com/demo/strtotime.php
0
 
LVL 58

Expert Comment

by:Gary
ID: 39957243
Ok yours works too and shorter.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now