To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 12-11-2003, 04:27 PM   #1
emrys404
Senior Member
 
Join Date: May 2003
Posts: 138
check for holiday function

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

PHP Code:

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;
    }
}
__________________
blar
emrys404 is offline   Reply With Quote
Old 12-11-2003, 06:50 PM   #2
saronoff
Member
 
Join Date: Jan 2003
Location: California
Posts: 30
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.
saronoff is offline   Reply With Quote
Old 12-11-2003, 08:07 PM   #3
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
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.
PHP Code:
$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.
PHP Code:
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.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 12-11-2003, 09:01 PM   #4
emrys404
Senior Member
 
Join Date: May 2003
Posts: 138
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
__________________
blar
emrys404 is offline   Reply With Quote
Old 12-24-2003, 11:15 AM   #5
fikse
Member
 
Join Date: Aug 2002
Posts: 56
here is your answer:

http://abledesign.com/programs/holiday_code.php
fikse is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 04:09 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.