Solved

Is this possible?

Posted on 2014-03-26
10
114 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 75

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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
 
LVL 109

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 75

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

832 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