shadedecho
Programmer
I'm trying to develop an algorithm (as clean as possible, I can think of some very dirty ways to approach it) that will allow my PHP code to inspect the current date and return the next date that is either the 1st or 3rd (and NOT the 5th, if one exists) monday of a month.
* So, if it's Tuesday June 3 (which is past the first monday, june 2), it should return the 3rd monday, which is June 16.
* if it's Thursday, June 26th (past the third monday, I want it to return July 7th (which is actually skipping the 5th monday in june -- june 30th -- and choosing the first monday of the next month).
My thought is to extract the current month, then using MySQL's date functions "count" up from the 1st of the month til a monday is found. Then, compare that to the current date, and if less than current date, add 2 weeks to it, and re-compare the date. If still less, than increment month by one, and find its first monday. Of course, this algorithm also has to check for things like the current month being 12, and actually having to go to month 1 of the next (incremented) year.
All that seems kinda dirty to me, wondering if there's a clean(er) way of doing so? Also had a thought that maybe I could use the fact that MySQL can return the week number a certain date falls in (either counting sunday as the first day or counting monday as the first day of a week). Then, with some dirty math, divide out those weeks and find out which week number the 1st or 3rd monday would begin in.
I know I will have to combine PHP with MySQL's DATE functions to get at this best...
But, either method seems really costly from a processing standpoint, as well as complicated to write and troubleshoot and maintain. Any one have a better thought on how to approach this?
* So, if it's Tuesday June 3 (which is past the first monday, june 2), it should return the 3rd monday, which is June 16.
* if it's Thursday, June 26th (past the third monday, I want it to return July 7th (which is actually skipping the 5th monday in june -- june 30th -- and choosing the first monday of the next month).
My thought is to extract the current month, then using MySQL's date functions "count" up from the 1st of the month til a monday is found. Then, compare that to the current date, and if less than current date, add 2 weeks to it, and re-compare the date. If still less, than increment month by one, and find its first monday. Of course, this algorithm also has to check for things like the current month being 12, and actually having to go to month 1 of the next (incremented) year.
All that seems kinda dirty to me, wondering if there's a clean(er) way of doing so? Also had a thought that maybe I could use the fact that MySQL can return the week number a certain date falls in (either counting sunday as the first day or counting monday as the first day of a week). Then, with some dirty math, divide out those weeks and find out which week number the 1st or 3rd monday would begin in.
I know I will have to combine PHP with MySQL's DATE functions to get at this best...
But, either method seems really costly from a processing standpoint, as well as complicated to write and troubleshoot and maintain. Any one have a better thought on how to approach this?