Jump to content

There has to be a better way to construct this?


DBookatay

Recommended Posts

I'm trying to create a holiday script, that shows a banner on a holiday, here is what I have for July 4th, but there has to be a better way to construct it:

 

if($date == "07-02" OR $date == "07-03" OR $date == "07-04" ){
   do what I want
}

Link to comment
Share on other sites

Anything having to do with dates would be better if you were using a php date variable or datetime object.  They provide functions that will let you take a "month/day" and turn those easily into regular dates or datetimes.  Then you can do range calculations which work.  You might also want to think about making the feature generic, and storing a table that schedules banners.  An easy/flexible way to handle that would be:

 

From, To, Banner

 

Then you need only create a generic banner script that determines the current date, checks to see if that date falls between any of the date ranges in the table (as suggested by requinix) and displays the appropriate banner, or falls through to some other banner.

 

The only thing you are left to have to decide upon is the storage format (flat file/database/nosql/hardcoded array).  If you wanted to opt for the array then something like this would work well:

 

// holiday banner schedule
$holidaybanner = array();
$holidaybanner[] = array('name' => '4th of July', 'fromMonth' => 7, 'fromDay' => 2, 'toMonth' => 7, 'toDay' => 4, 'banner' => '/path/to/independence.png');
$holidaybanner[] = array('name' => 'Christmas', 'fromMonth' => 12, 'fromDay' => 23, 'toMonth' => 12, 'toDay' => 30, 'banner' => '/path/to/christmas.png');

 

 

Of course the problem with this is that certain holidays do not fall on the same dates, but you could engineer in some additional sophistication, and allow for actual date ranges for specific years.

New years is kind of tricky as well, but that is the fun of programming.

Link to comment
Share on other sites

I don't know how it works over in the US but here in the UK moveable  holidays fall on the first or last Monday of a month ( first and last in May, last in August) so these can be determined for any given year.

 

The really tricky one is Easter which wanders erratically, but thankfully PHP provides us with a function to return Easter Sunday date ( easter_date($year) )

Link to comment
Share on other sites

Here is a listing of holidays that I found buried deep in my library.  Couple of years old, may need some added.

if($monthName == 'January') { //New Years
$holiday[$year]['01'][1]['New Years Day']['First Day of the Year'] = './images/flag.png';
$store_days[] = 1;
}
elseif($monthName == 'March' || $monthName == 'April') { //Easter
$easter = explode(' ' ,date('m d Y', easter_date($year)));		
	$holiday[$easter[2]][$easter[0]][$easter[1]]['Easter Sunday']['Easter Sunday'] = './images/egg.png';
}
elseif($monthName == 'May') { //Mothers Day, Memorial Day.
$mothersDay = date('j', strtotime('+1 week Sunday ' . $monthName . ' ' . $year));
$holiday[$year]['05'][$mothersDay]['Mothers Day']['Kiss your mother, it is her day.'] = './images/mother.png';
$memorialDay = (($mothersDay + 22) > $days_in_month) ? ($mothersDay + 22) - 7 : $mothersDay + 22;
$holiday[$year]['05'][$memorialDay]['Memorial Day']['Support the Troops.'] = './images/soldier.png';
}
elseif($monthName == 'June') { //Fathers Day
$fathersDay = date('j',strtotime('+2 week Sunday June ' . $year));
$holiday[$year]['06'][$fathersDay]['Fathers Day']['Hug your father, it is his day.'] = './images/father.png';
}
elseif($monthName == 'July') { //Independence Day
$holiday[$year]['07'][4]['Independence Day']['Celebrate Freedom'] = './images/flag.png';
}
elseif($monthName == 'September') { //Patriot Day
$holiday[$year]['09'][11]['Patriot Day']['Memorial of the Twin Towers'] = './images/patriot.png';
$laborDay = date('j',strtotime('+0 week Monday September ' . $year));
$holiday[$year]['09'][$laborDay]['Labor Day']['National Holiday'] = './images/worker.png';
}
elseif($monthName == 'November') { //Thanksgiviing
switch($day_of_week){
	case "Sun": $add = 25; break;
	case "Mon": $add = 24; break;
	case "Tue": $add = 23; break;
	case "Wed": $add = 22; break;
	case "Thu": $add = 21; break;
	case "Fri": $add = 27; break;
	case "Sat": $add = 26; break;
	}
$thanksgiving = $add + 1;
$holiday[$year][11][$thanksgiving]['Thanksgiving Day']['Give thanks for everything we have.'] = './images/turkey.png';
}
elseif($monthName == 'December') { //Christmas
$holiday[$year][12][25]['Christmas Day']['Celebrating the Birth of Jesus Christ'] = './images/ctree.png';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.