FatherCarbon Posted July 16, 2009 Share Posted July 16, 2009 Hey everyone, I've been scratching my head over this for a couple days. I simply want to have a function to return 4 specific holidays (Memorial Day, July 4th, Thanksgiving, and Christmas). Of course the 4th and Christmas are no problem. I was looking for a way to calculate floating holidays and came across this solution for Thanksgiving: http://php.about.com/od/finishedphp1/qt/thanksgiving.htm That works great, but after I modify it a bit to work with Memorial Day, it always returns the 4th Monday instead of the last Monday in May for years where May has 5 Mondays. I need the day Memorial Day is observed otherwise I could just put '05/30' in the script. For example, everything I've tried returns 05/24 for 2010 when in fact it's 05/31 for that year. If anyone can point me in the right direction here, I will give you the heart straight from my chest! Link to comment Share on other sites More sharing options...
ignace Posted July 16, 2009 Share Posted July 16, 2009 $year = date('Y') + 1; // get the number of days within a specific month $number_of_days = date('t', mktime(0, 0, 0, 5, 1, $year)); // go over each day starting from the last and.. do { $weekday = date('N', mktime(0, 0, 0, 5, $number_of_days, $year)); $number_of_days--; // ..stop on the first occurence of Monday } while ($weekday != 1); $day = $number_of_days + 1; print 'Memorial day 05/' . $day . '/' . $year; Link to comment Share on other sites More sharing options...
FatherCarbon Posted July 16, 2009 Author Share Posted July 16, 2009 You're my hero, ignace! Thanks so much, seriously! Link to comment Share on other sites More sharing options...
hmarc Posted January 22, 2015 Share Posted January 22, 2015 (edited) It's easier to just use the date() function: list($Memorial,$Labor) = getMemorialLaborDays); // ==================================================== // Compute and return YYYY-MM-DD for Memorial Day and // Labor Day of the current year. Both are Mondays. // ==================================================== function getMemorialLaborDays() { $M = 31; while (date("N",strtotime("May $M")) != 1) { --$M; } $L = 1; while (date("N",strtotime("September $L")) != 1) { ++$L; } $L = sprintf("%02d",$L); return(Array(date("Y-05-$M"),date("Y-09-$L"))); } Edited January 22, 2015 by hmarc Link to comment Share on other sites More sharing options...
Barand Posted January 23, 2015 Share Posted January 23, 2015 There is an easier way echo (new DateTime("Last monday of May"))->format("Y-m-d"); //--> 2015-05-25 echo (new DateTime("Last thursday of November"))->format("Y-m-d"); //--> 2015-11-26 Link to comment Share on other sites More sharing options...
ignace Posted January 24, 2015 Share Posted January 24, 2015 Old topic. Closing. Link to comment Share on other sites More sharing options...
Recommended Posts