Failure detecting Memorial Day

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

I have some code in one of my web sites to detect holidays. In
particular, Memorial Day didn't work out. Here's a test:

\$month = 5;
\$day = 31;
\$year = 2010;
\$today = mktime(1,1,1, \$month, \$day, \$year);
\$holiday = strtotime("last monday", \$today);

Normally, on Memorial Day, \$today == \$holiday. But when the last day
of the month is also the last last monday of the month, \$holiday is
returned as the prior Monday, so \$today != \$holiday. That's what
happened last week.

How would I use strtotime() to test if today is the last Monday so
that it works all the time, even when the last day of the month is
the last Monday of the month?

-A

Re: Failure detecting Memorial Day

axlq wrote:

'last monday' refers to the monday previous to the specified date, not
the last monday of the month.

You could compute Memorial Day with something like:

date( 'm/d/Y', strtotime( '-1 week monday', strtotime('June 1 2010')));

This goes back 1 week from 6/1/2010 and looks for the next Monday.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Failure detecting Memorial Day

Ah. Thanks Jerry. That makes sense.

-A

Re: Failure detecting Memorial Day

On 08/06/10 00:35, axlq wrote:

<?php
\$d = "31 May ".date(Y);
echo "<p>Testing: \$d</p>";
if (date(N,strtotime(\$d)) == 1) {
echo "<p>31st may is Mon this year</p>";
}
else {
echo "<p>31st may is not Mon this year</p>";
}
?>

Rgds

Denis McMahon

Re: Failure detecting Memorial Day

On 6/7/2010 7:35 PM, axlq wrote:

would you be willing to share your code for detecting holidays ?
I keep a dog show calendar and now manually enter the holidays,
but seem to miss one every year.

bill

Re: Failure detecting Memorial Day

El 08/06/2010 12:48, bill escribió/wrote:

In Spain, the exact festive days are decided by the government on
previous December. Although most of them are pretty fixed, there's
really no way to tell out in advance. But I suppose you live somewhere
else :)

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Re: Failure detecting Memorial Day

Certainly. Here it is, using Jerry Stuckle's suggested correction
for Memorial Day. I don't remember where I got the original version
of this from.

Also, the first function that detects Good Friday is something I
found before I discovered that php has an easter_date() function.
The expression strtotime("last friday",easter_date(\$year)); will
return the date of Good Friday too.

============================================

<?php

function is_good_friday(\$date) {

// The following calculation of the Date for Easter is based on
// Oudin's algorithm for determining the date of Easter and follows
// the algorithm given in the New Scientist magazine, issue No. 228
// (Vol. 9) page 828 (March 1961). It was originally coded in
// Multics PL/1 by Dennis Capps in 9/80 to fix bug a in the easter
// calculation in a program called calendar originally written by
// Tom Van Vleck of MIT in 1972.  It was converted to Crystal and
// modified to return the date of Good Friday by Mike Cook in 2003.
// The comments are mostly from the original PL/1.It should be valid
// for any date from 1583 to 4099, so we may have a year 4K issue.

\$yr = date('Y', \$date);
\$a = \$yr % 19;
\$b = intval(\$yr / 100);
\$c = \$yr % 100;
\$d = intval(\$b / 4);
\$e = \$b % 4;
\$i = intval(\$c / 4);
\$k = \$c % 4;
\$g = intval((8 * \$b + 13) / 25);
\$h = ((19 * \$a) + \$b - \$d - \$g + 15) % 30;
\$l = ((2 * \$e) + (2 * \$i) - \$k + 32 - \$h) % 7;
\$m = intval((\$a + (11*\$h) + (19*\$l)) / 433);
\$days_to_gf = \$h + \$l - (7*\$m) - 2;
\$mo = intval((\$days_to_gf + 90) / 25);
\$da = (\$days_to_gf + (33 * \$mo) + 19) % 32;
if (mktime(0,0,0,\$mo,\$da,\$yr) == \$date)
return 'Good Friday';
return false;
}

function is_holiday(\$date)
{
\$year = date('Y', \$date);
\$holidays = array(
"New Years Day" =>   array("today", 1, 1),
"MLK Jr. Day" =>     array("3 monday", 1, 1),
"President's day" => array("3 monday", 2, 1),
// Good Friday is special case
"Memorial Day" =>    array("-1 week monday", 6, 1),
"Independence Day" => array("today", 7, 4),
"Labor Day" =>       array("1 monday", 9, 1),
"Columbus Day" =>    array("1 week monday", 10, 1),
"Veterans Day" =>    array("today", 11, 11),
"Thanksgiving" =>    array("4 thursday", 11, 1),
"Christmas" =>       array("today", 12, 25),
);
foreach(\$holidays as \$name => \$hldy) {
\$hldy_time = mktime(1,1,1, \$hldy[1], \$hldy[2], \$year);
if (strtotime(\$hldy[0], \$hldy_time) == \$date)
return \$name;
}
return is_good_friday(\$date);
}

?>

============================================

The code above is somewhat modified from what I use, because I had
some application specific things in there. I modified it in the
newsreader so I hope no typos crept in.

-A

Re: Failure detecting Memorial Day

On 6/8/2010 3:43 PM, axlq wrote:

very much appreciated.
Bill