note 48770 added to function.mktime

From: Date: Thu, 06 Jan 2005 16:42:40 +0000
Subject: note 48770 added to function.mktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-82851@lists.php.net to get a copy of this message
I have found several samples on the net on how to find holidays - one in particular being http://www.smart.net/~mmontes/ushols.html (which is a great bit of info on the subject including algorithms) using that as a base, I wrote this to find the base holidays using mktime: // nth_wkday_of_month() {returns 1-31 or -1 if date is greater than number of days in month} // to get thanksgiving 2004, it is 4th Thursday of 2004 so numday=4(4th), wkday=4(Thursday) :: returns 25 (then compare with month 11) function nth_wkday_of_month($numweek,$wkday,$mm,$yyyy) { $day_offset = ($wkday-date('w',mktime(0,0,0,$mm,1,$yyyy))+7)%7; if ($day_offset>6) $day_offset -= 7; $nth_day = 1+($numweek-1)*7+$day_offset; return $nth_day<=date('d',mktime(0,0,0,$mm+1,0,$yyyy))?$nth_day:-1; } // last_wkday_of_month() // Example: last_wkday_of_month(3,2,2005) returns 23 (The last Wednesday in February, 2005 is the 23rd) function last_wkday_of_month($wkday,$mm,$yyyy) { $day_offset = ($wkday-date('w',mktime(0,0,0,$mm,1,$yyyy))+7)%7; if ($day_offset>6) $day_offset -= 7; $last_wkday = 29+$day_offset; if ($last_wkday>date('d',mktime(0,0,0,$mm+1,0,$yyyy))) $last_wkday-=7; return $last_wkday; } function is_holiday($mm,$dd,$yyyy) { // this function requires the nth_wkday_of_month() and last_wkday_of_month() functions // additional holidays can be added easily // returns false if not a holiday, or name of holiday that falls on the specified date // simple check for 'dated' holidays if ($mm==12 && $dd==25) return "Christmas"; if ($mm==7 && $dd==4) return "Independence Day"; if ($mm==1 && $dd==1) return "New Year's Day"; // check for 'weekday-based' holidays if ($mm==10 && $dd==nth_wkday_of_month(2,1,$mm,$yyyy)) return "Columbus Day"; // 2nd Monday in October [10-10-2005] if ($mm==9 && $dd==nth_wkday_of_month(1,1,$mm,$yyyy)) return "Labor Day"; // 1st Monday in September [09-05-2005] if ($mm==1 && $dd==nth_wkday_of_month(3,1,$mm,$yyyy)) return "Martin Luther King Day"; // 3rd Monday in January [01-17-2005] if ($mm==2 && $dd==nth_wkday_of_month(3,1,$mm,$yyyy)) return "Presidents' Day"; // 3rd Monday in February [02-21-2005] if ($mm==11 && $dd==nth_wkday_of_month(4,4,$mm,$yyyy)) return "Thanksgiving"; // 4th Thursday in November [11-24-2005] if ($mm==11 && $dd==nth_wkday_of_month(2,1,$mm,$yyyy)) return "Veterans' Day"; // 2nd Monday in November [11-14-2005] if ($mm==5 && $dd==last_wkday_of_month(1,$mm,$yyyy)) return "Memorial Day"; // Last Monday in May [05-30-2005] return false; } EXAMPLE USAGE: $holiday = is_holiday(date('n'),date('j'),date('Y')); if ($holiday) echo "It is $holiday today."; -- so is_holiday(12,25,2004) responds: It is Christmas today. I hope this is useful ---- Manual Page -- http://www.php.net/manual/en/function.mktime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48770 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48770&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48770&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48770&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48770&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48770&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48770&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48770&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#82851) next »