2014-01-15 58 views
1

,我發現在網絡上一些隨機代碼,並使用它爲我的日曆試過,但我不斷收到此錯誤:PHP節假日

Notice: Undefined variable: nextHoliday 

這個錯誤是指代碼底「返回$ nextHoliday;「

我相信nextHoliday定義壽所以我嘗試了一些東西,但沒有使它的工作..可有人請幫忙嗎?

下面的代碼:

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"].")"; 
+2

誰寫的代碼有一個很好的貨物崇拜編程的案例和迷戀過度使用'shift'和/或'caps-lock'鍵...... –

+0

也許開始下一個假期首先在'if'作爲'$ nextHoliday = array();' – Albzi

+0

我曾嘗試過,並得到了錯誤..注意:未定義偏移量:8 – rubberchicken

回答

2

你有兩種選擇之一。無論是實例空鍵爲您的數組值:每個陣列查找之前

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

或者使用isset:

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

比內嵌條件語句三元運營商更好地沒什麼。

實際上我們在1月份測試了這個功能,因爲否則這個錯誤會在以後發生。問題在於,您使用的是小於/大於以確定下一個假期是什麼。這沒有考慮去年的最後一個假期。

爲了解決這個問題,變量$ lastHoliday需要是最後一個節日的負表示:

$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; 
    } 
} 

也可以考慮在小寫輸入PHP,沒有大寫,因爲它是在PHP幾乎是普遍的標準。一個真正的支架風格也不會傷害。

+0

是的,現在顯示空的結果而不是錯誤,但我認爲它是假設從列表中顯示下一個即將到來的假期?這應該是情人節...... – rubberchicken

+0

我應該認爲這是算法,而不是隻是修復錯誤。我會仔細閱讀,看看我能否弄清楚。 – NobleUplift

+0

非常感謝。 – rubberchicken

2

$ nextHoliday以使用,如果但不申報

之前聲明變量:

$nextHoliday = array(); 
+0

解析錯誤:語法錯誤,意外T_ARRAY – rubberchicken

+0

也許'新'不支持:s – R3tep

+0

注意:未定義的索引:名稱 – rubberchicken