Jump to content

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


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:

$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;

  • 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

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

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.