Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combination of MySQL DATE functions and PHP to get specific dates... 1

Status
Not open for further replies.

shadedecho

Programmer
Oct 4, 2002
336
US
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?
 
Why use MySQL functions at all?

The Date PEAR package provides a class Date_Calc which has a method NWeekdayOfMonth().

Here's a snippet from the class script:

/**
* Calculates the date of the Nth weekday of the month,
* such as the second Saturday of January 2000.
*
* @param string occurance: 1=first, 2=second, 3=third, etc.
* @param string dayOfWeek: 0=Sunday, 1=Monday, etc.
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
*
* @return string date in given format
*/

function NWeekdayOfMonth($occurance,$dayOfWeek,$month,$year,$format="%Y%m%d")$dayOfWeek,$month,$year,$format="%Y%m%d")

Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks for that great suggestion, you get a star!

Unfortunately, we're running on 4.0.6 right now, which didn't ship packaged with PEAR, but we plan to upgrade soon, and so when we do, I'll make sure to test out your solution!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top