DBookatay Posted July 1, 2012 Share Posted July 1, 2012 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 } Quote Link to comment https://forums.phpfreaks.com/topic/265065-there-has-to-be-a-better-way-to-construct-this/ Share on other sites More sharing options...
requinix Posted July 1, 2012 Share Posted July 1, 2012 Not really. You could express it as a date range with if($date >= "07-02" && $date or an array if you wanted to stick in a whole bunch of dates [code=php:0]if(in_array($date, array("07-02", "07-03", "07-04"))){ Quote Link to comment https://forums.phpfreaks.com/topic/265065-there-has-to-be-a-better-way-to-construct-this/#findComment-1358299 Share on other sites More sharing options...
Rage Posted July 1, 2012 Share Posted July 1, 2012 i would personally put it in a switch so you can have different banners for different days not the cleanest code but it gets the job done Quote Link to comment https://forums.phpfreaks.com/topic/265065-there-has-to-be-a-better-way-to-construct-this/#findComment-1358306 Share on other sites More sharing options...
gizmola Posted July 1, 2012 Share Posted July 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265065-there-has-to-be-a-better-way-to-construct-this/#findComment-1358307 Share on other sites More sharing options...
Barand Posted July 1, 2012 Share Posted July 1, 2012 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) ) Quote Link to comment https://forums.phpfreaks.com/topic/265065-there-has-to-be-a-better-way-to-construct-this/#findComment-1358331 Share on other sites More sharing options...
jcbones Posted July 1, 2012 Share Posted July 1, 2012 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/265065-there-has-to-be-a-better-way-to-construct-this/#findComment-1358357 Share on other sites More sharing options...
DBookatay Posted July 1, 2012 Author Share Posted July 1, 2012 Wow thank you, that saves me from having to write a lot... Quote Link to comment https://forums.phpfreaks.com/topic/265065-there-has-to-be-a-better-way-to-construct-this/#findComment-1358366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.