1

i found some random code on the net and tried using it for my calendar but i keep getting this error:

Notice: Undefined variable: nextHoliday

this error is referring to the code at the end "RETURN $nextHoliday; "

I believe nextHoliday is defined tho so i tried a few things but nothing makes it work.. can someone please help?

Here's the code:

FUNCTION GetTimeStamp($MySqlDate) 
  { 


  $date_array = EXPLODE("-",$MySqlDate); // split the array 

  $var_year = $date_array[0]; 
  $var_month = $date_array[1]; 
  $var_day = $date_array[2]; 

  $var_timestamp = MKTIME(0,0,0,$var_month,$var_day,$var_year); 
  RETURN($var_timestamp); // return it to the user 
  }  // End function GetTimeStamp() 

FUNCTION ordinalDay($ord, $day, $month, $year) 
  // ordinalDay returns date of the $ord $day of $month. 
  // For example ordinalDay(3, 'Sun', 5, 2001) returns the 
  // date of the 3rd Sunday of May (ie. Mother's Day). 
  // 
  // Note: $day must be the 3 char abbr. for the day, as 
  //       given by date("D"); 
  // 

  { 
  $firstOfMonth = GetTimeStamp("$year-$month-01"); 
  $lastOfMonth  = $firstOfMonth + DATE("t", $firstOfMonth) * 86400; 
  $dayOccurs = 0; 

  FOR ($i = $firstOfMonth; $i < $lastOfMonth ; $i += 86400) 
     { 
     IF (DATE("D", $i) == $day) 
       { 
       $dayOccurs++; 
       IF ($dayOccurs == $ord) 
         { $ordDay = $i; } 
       } 
     } 
  RETURN $ordDay; 
  }  // End function ordinalDay() 

FUNCTION getNextHoliday() 
   // Looks through a lists of defined holidays and tells you which 
   // one is coming up next. 
   // 
   { 
   $year = DATE("Y"); 

   CLASS holiday 
     { 
     VAR $name; 
     VAR $date; 
     VAR $catNum; 

     FUNCTION holiday($name, $date, $catNum) 
        // Contructor to define the details of each holiday as it is created. 
        { 
        $this->name   = $name;   // Official name of holiday 
        $this->date   = $date;   // UNIX timestamp of date 
        $this->catNum = $catNum; // category, we used for databases access 
        } 
     } // end class holiday 

   $holidays[] = NEW holiday("Groundhog Day", GetTimeStamp("$year-2-2"), "20"); 
   $holidays[] = NEW holiday("Valentine's Day", GetTimeStamp("$year-2-14"), "14"); 
   $holidays[] = NEW holiday("St. Patrick's Day", GetTimeStamp("$year-3-17"), "15"); 
   $holidays[] = NEW holiday("Easter", EASTER_DATE($year), "16"); 
   $holidays[] = NEW holiday("Mother's Day", ordinalDay(2, 'Sun', 5, $year), "3"); 
   $holidays[] = NEW holiday("Father's Day", ordinalDay(3, 'Sun', 6, $year), "4"); 
   $holidays[] = NEW holiday("Independence Day", GetTimeStamp("$year-7-4"), "17"); 
   $holidays[] = NEW holiday("Christmas", GetTimeStamp("$year-12-25"), "13"); 

   $numHolidays = COUNT($holidays); 
   FOR ($i = 0; $i < $numHolidays; $i++) 
     { 
     IF ( DATE("z") > DATE("z", $holidays[$i]->date) && DATE("z") <= DATE("z", 
          $holidays[$i+1]->date) ) 
        { 
        $nextHoliday["name"]      = $holidays[$i+1]->name; 
        $nextHoliday["dateStamp"] = $holidays[$i+1]->date; 
        $nextHoliday["dateText"]  = DATE("F j, Y", $nextHoliday["dateStamp"]); 
        $nextHoliday["num"]       = $holidays[$i+1]->catNum;         
        } 
     } 
   RETURN $nextHoliday; 
   } // end function GetNextHoliday 


$nextHoliday = getNextHoliday(); 
ECHO $nextHoliday["name"]." (".$nextHoliday["dateText"].")"; 
4
  • 2
    Whoever wrote that code has a nice case of cargo-cult programming and a fetish for overuse of 'shift' and/or 'caps-lock' keys... Commented Jan 15, 2014 at 15:17
  • Maybe start next holiday first of all before the if as $nextHoliday = array(); Commented Jan 15, 2014 at 15:20
  • i tried that before and got errors.. Notice: Undefined offset: 8 Commented Jan 15, 2014 at 15:35
  • For some reason PHP in all caps (though legal) angers me... also, the array should be defined above the for loop. Commented Jan 15, 2014 at 16:25

2 Answers 2

2

You have one of two options. Either instantiate empty keys for your array values:

$nextHoliday = array();
$nextHoliday['name'] = '';
$nextHoliday['dateStamp'] = '';
$nextHoliday['dateText'] = '';
$nextHoliday['num'] = '';
$numHolidays = COUNT($holidays); 
for ($i = 0; $i < $numHolidays; $i++) {
    // ... Blah
}

Or use isset before each array lookup:

echo (isset($nextHoliday['name'] ? $nextHoliday['name'] : '') . 
" (" . 
(isset($nextHoliday) ? $nextHoliday["dateText"] : '' ) . 
")";

Nothing better than the ternary operator for inline conditionals.

It's actually good that we're testing this in January, because otherwise this bug would have bitten you later on. The problem is that you are using less than/greater than to determine what the next holiday is. This fails to take into account the last holiday of the last year.

To fix this, the variable $lastHoliday needs to be a negative representation of the last holiday:

$numHolidays = COUNT($holidays);
$nextHoliday = array('name' => '', 'dateStamp' => '', 'dateText' => '', 'num' => '');
for ($i = 0; $i < $numHolidays - 1; $i++) {
    $today = DATE("z");
    if ($i == 0) {
        $lastHoliday = (365 - DATE("z", $holidays[$numHolidays - 1]->date)) * -1;
    } else {
        $lastHoliday = DATE("z", $holidays[$i]->date);
    }
    $futureHoliday = DATE("z", $holidays[$i+1]->date);

    //print_r($today); echo "<br />";
    //print_r($lastHoliday); echo "<br />";
    //print_r($futureHoliday); echo "<br />";

    if ($today > $lastHoliday && $today <= $futureHoliday ) {
        $nextHoliday["name"]      = $holidays[$i+1]->name;
        $nextHoliday["dateStamp"] = $holidays[$i+1]->date;
        $nextHoliday["dateText"]  = DATE("F j, Y", $nextHoliday["dateStamp"]);
        $nextHoliday["num"]       = $holidays[$i+1]->catNum;
    }
}

Also consider typing PHP in lowercase, not uppercase, as it is an almost universal standard in PHP. One true brace style wouldn't hurt either.

Sign up to request clarification or add additional context in comments.

3 Comments

yes this displays empty result now instead of error, although i thought it is suppose to display the next upcoming holiday from the list? which should be valentines day...
I should have assumed it was algorithmic instead of just fixing errors. I'll read over it and see if I can figure it out.
There, the algorithm should be fixed now.
2

$nextHoliday is used in a if but is not declared

Declare the variable before the for:

[...]
$nextHoliday = array();
FOR ($i = 0; $i < $numHolidays; $i++) 
[...]

3 Comments

Maybe the 'new' is not supported :s
Notice: Undefined index: name
it should be returning the next holiday tho...why isnt it ? if it did return the next holiday there would be no error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.