Page 1 of 1

(help) Modify date class to display all entries in list.

Posted: Tue Sep 27, 2005 12:12 pm
by carnuke
The following class displays a single line of text according to todays date. I want to change this so that it displays all of the entries as a summary list. eg;

0101- Happy new year
2709- Happy Groundhog's Day
0214- Happy Valentine's Day

etc ...

Can you help please?

TIA :-)

Code: Select all

<?php

// Set date/time variables.
$time=getdate();
$hour=$time['hours'];
$day=$time['mday'];
$month=$time['mon'];
$wday=$time['wday'];
$wdnum=(int)(($day+6)/7);
if (strlen($month)==1){$mm="0".$month;}
else {$mm=$month;}
if (strlen($day)==1){$dd="0".$day;}
else {$dd=$day;}
$mmdd=$mm.$dd;
$nwmm=$wdnum.$wday.$mm;

// Check for holidays with set dates.
switch ($mmdd){
case "1201":
case "0101":
$greet="Happy New Year";
break;
case "2709":
$greet="Happy Groundhog's Day";
break;
case "0214":
$greet="Happy Valentine's Day";
break;
case "0317":
$greet="Happy St. Patrick's Day";
break;
case "0422":
$greet="Happy Earth Day";
break;
case "0614":
$greet="Happy Flag Day";
break;
case "0704":
$greet="Happy Independence Day";
break;
case "1031":
$greet="Happy Halloween";
break;
case "1111":
$greet="Happy Veteran's Day";
break;
case "1225":
$greet="Merry Christmas";
break;
default:
unset($mmdd);
break;}

// Check for holidays with variable dates.
switch ($nwmm){
case "3102":
$greet="Happy President's Day";
break;
case "2005":
$greet="Happy Mother's Day";
break;
case "3006":
$greet="Happy Father's Day";
break;
case "1109":
$greet="Happy Labor Day";
break;
case "4511":
$greet="Happy Thanksgiving";
break;
default:
unset($nwmm);
break;}

// Check for Memorial Day or default to a standard daily greeting.
if (isset($greet)){}
elseif ($mm==05&&$dd>24&&$wday==1){$greet="Happy Memorial Day";}
elseif (empty($mmdd)&&empty($nwmm)){
	if ($hour<=4){$greet="Good evening";}
	elseif ($hour>=5&&$hour<12){$greet="Good morning";}
	elseif ($hour>=12&&$hour<18){$greet="Good afternoon";}
	elseif ($hour>=18){$greet="Good evening";}}
else {$greet="Hello";}

echo $greet;

?>

Posted: Tue Sep 27, 2005 12:17 pm
by feyd
convert the dates and "events" into an array. Lose the switch by simply seeing if the current date has a key in the array. You can have a seperate method to display the array itself..

Posted: Tue Sep 27, 2005 1:06 pm
by s.dot
I think what I would do is store your date and special day in an array with the dates as keys, and the day as the value

like

Code: Select all

Array (
  [0101] => Happy New Years
  [0214] => Happy Valentines Day
)
Then get the current data

Code: Select all

$date = date('md');
Then get the array keys of your array

Code: Select all

$keys = array_keys($yourarray)
check to see if today's date is in the array

Code: Select all

if(in_array($date,$keys)
{
   // the day IS a special day, so do whatever
   echo $yourarray[$date]; // this would print the message like 'Happy New Years', etc.
}

Posted: Tue Sep 27, 2005 1:11 pm
by feyd

Posted: Tue Sep 27, 2005 1:16 pm
by s.dot
feyd wrote:pssst.. array_key_exists() ;)
Thus making my example a useless extension of your post. :P

Thanks

Posted: Wed Sep 28, 2005 5:50 am
by carnuke
Thanks guys, Im sure I can do something with that :)