Jump to content

[SOLVED] Calculating Memorial Day (last Monday in May) for a given year?


FatherCarbon

Recommended Posts

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!  :confused:

Link to comment
Share on other sites

$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

  • 5 years later...

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 by hmarc
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.