PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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
<?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;
?>
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..
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.
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Thus making my example a useless extension of your post.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.