A More Elegant Programming Method?

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
Althius
Forum Newbie
Posts: 2
Joined: Wed Feb 22, 2006 3:12 pm

A More Elegant Programming Method?

Post by Althius »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I am trying to figure out what holiday we are currently closest to, without going over.  This works, but it seems like there was a better way than this.  Does anyone have any suggestions?

Code: Select all

$date = getdate(); // Get Today's Date
$dateStamp = mktime (0, 0, 0, $date['mon'], $date['mday'], $date['year']); // Convert it into a Timestamp

$imgName = setHoliday($dateStamp); // Run the function and set the imgName
echo $imgName;

function setHoliday($d) {
	$valentines = mktime (23, 59, 0, 2, 14, 2006);
	$stpatricks = mktime (23, 59, 0, 3, 17, 2006);
	$easter = mktime (23, 59, 0, 4, 16, 2006);
	$mothersDay = mktime (23, 59, 0, 5, 14, 2006);
	$fourthOfJuly = mktime (23, 59, 0, 7, 4, 2006);
	$halloween = mktime (23, 59, 0, 10, 31, 2006);
	$thanksgiving = mktime (23, 59, 0, 11, 24, 2006);
	$christmas = mktime (23, 59, 0, 12, 31, 2006);	
	
	if ($d<=$valentines) {
		$i = 'valentines';
	}
	else {
		if ($d<=$stpatricks) {
			$i = 'stpatricks';
		}
		else {
			if ($d<=$easter) {
				$i = 'easter';
			}
			else {
				if ($d<=$mothersDay) {
					$i ='mothersday';
				}
				else {
					if ($d<=$fourthOfJuly) {
						$i = 'fourthofjuly';
					}
					else {
						if ($d<=$halloween) {
							$i = 'halloween';
						}
						else {
							if ($d<=$thanksgiving) {
								$i = 'thanksgiving';
							}
							else {
								if ($d<=$christmas) {
									$i = 'christmas';
								}
							}
						}
					}
				}
			}
		}					
	}
	return $i;
}
Thanks in advance


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

option 1:

Code: Select all

if()
{
  ;
}
elseif()
{
  ;
}
else
{
  ;
}
option 2

Code: Select all

function setHoliday($d) {
    $times = array();
    $times['valentines'] = mktime (23, 59, 0, 2, 14, 2006);
    $times['stpatricks'] = mktime (23, 59, 0, 3, 17, 2006);
    $times['easter'] = mktime (23, 59, 0, 4, 16, 2006);
    $times['mothersDay'] = mktime (23, 59, 0, 5, 14, 2006);
    $times['fourthOfJuly'] = mktime (23, 59, 0, 7, 4, 2006);
    $times['halloween'] = mktime (23, 59, 0, 10, 31, 2006);
    $times['thanksgiving'] = mktime (23, 59, 0, 11, 24, 2006);
    $times['christmas'] = mktime (23, 59, 0, 12, 31, 2006);    
    
    asort($times);
    $i = 'unknown';
    foreach($times as $name => $time)
    {
    	if($d <= $time)
    	{
    		$i = $name;
    		break;
    	}
    }
    
    return $i;
}
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post by RobertPaul »

You could just get rid of all the else statements, and have one if statement after another. It'd have the same effect.

And, FYI, to get $dateStamp you could just use strtotime("today") ... or mktime(0, 0, 0, date('n'), date('j'), date('Y')).
Althius
Forum Newbie
Posts: 2
Joined: Wed Feb 22, 2006 3:12 pm

Post by Althius »

Ah... I like #2 very much. I think that was what my brain was trying to do from the start, I just wasn't quite able to put it into practice. Thanks, and sorry for putting 'code' instead of 'php'.
Post Reply