emrys404
12-11-2003, 04:27 PM
This is my check for holiday function. Anyway i'm wondering what you think. Can i get some pointers on better ways to do it? Thanks.
-emrys
function checkholiday($date)
{
// $date will be in YYYY-MM-DD format
// if date falls on a holiday, then date is moved back one day
switch(date("m d",strtotime($date)))
{
case "12 25": //christmas
case "11 27": //thanksgiving
case "07 04": // 4th of July
case "01 01": // new years
return date("Y-m-d",strtotime("$date -1 day")); break;
default: return $date;
}
}
saronoff
12-11-2003, 06:50 PM
The only thing I would say you may want to look at is not hard-coding the list of holidays. Maybe the creation of another function that would return a listing of holidays so you could call it, search the results returned, and return whether you found a match. Other than that everything looks good.
Weedpacket
12-11-2003, 08:07 PM
You could set up an array indexed by date, and the value could be e.g., the name of the holiday, or even something more complex.
$holidays = array(
'12 25' => 'christmas',
'11 27' => 'thanksgiving',
'07 04' => '4th of July',
'01 01' => 'new years',
);
$date=date('m d', strtotime($date));
if(key_exists($date))
{...whatever is appropriate
}
$holidays of course doesn't need to be hardcoded, or at least, not built right into the script; it could be in a separate file that is included, or it could be drawn from a database, or it could actually be generated on the fly - starting with the statutories, and then adding "days off" that are scheduled on an ad-hoc basis.
"Whatever is appropriate" in this case is to return the previous date. Ah, but what if that's a holiday as well? Perhaps it should return checkholiday(date('Y-m-d', strtotime("$date -1 day")); instead :)- no, really, that would work.
function checkholiday($date, $holidays)
{
// $date will be in YYYY-MM-DD format
// if date falls on a holiday, then date is moved back to the first non-holiday
$md = date("m d",strtotime($date));
if(array_key_exists($md, $holidays)
return checkholiday(date("Y-m-d",strtotime("$date -1 day")), $holidays);
return $date;
}
It would be a bit on the slow side if holidays stretched on for a couple of weeks and you happened to ask for a day near the end (because of the repeated use of date() and strtotime() but since multi-day holidays are less common, that won't happen so much, and putting together a loop in the function that would keep checking back and back and back until it finds a non-holiday would probably involve more work than it saves.
Later, this passing of $holidays around might get tiring, and you could look at putting together a calendar class in which $holidays becomes a property and checkholiday becomes a method.
emrys404
12-11-2003, 09:01 PM
Thanks for the feedback guys. Making it an array and passing that around, and letting elements be added and removed would be very useful for my current project. Making a calendar class might be a bit involved at this point, but definetly worth creating for future projects.... hmm. Thanks
-emrys
fikse
12-24-2003, 11:15 AM
here is your answer:
http://abledesign.com/programs/holiday_code.php