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

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!

Moderator: General Moderators

Post Reply
carnuke
Forum Newbie
Posts: 4
Joined: Tue Sep 27, 2005 12:03 pm

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

Post 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;

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
}
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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

feyd wrote:pssst.. array_key_exists() ;)
Thus making my example a useless extension of your post. :P
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.
carnuke
Forum Newbie
Posts: 4
Joined: Tue Sep 27, 2005 12:03 pm

Thanks

Post by carnuke »

Thanks guys, Im sure I can do something with that :)
Post Reply