2015-01-09 87 views
0

我有一個函數,用於檢查當前日期是否落在數組中的多個日期區間之間。我唯一需要的是這個函數返回checkRange函數中的$ location值。PHP如何從函數中獲取值,用於檢查數組中的多個日期範圍

function promoDates(){ 
    $current = strtotime("now"); 

    // Array gives a place, start date, and end date 
    $intervals = array(
     //The $current start time falls between the dates in washington array 
     array('washington', strtotime("2015-01-08 00:00"), strtotime("2015-01-30 00:00")), 
     array('california', strtotime("2015-06-02 00:00"), strtotime("2015-06-17 00:00")), 
     array('texas', strtotime("2015-02-12 00:00"), strtotime("2015-02-27 00:00")), 
     array('ney-york', strtotime("2015-05-12 00:00"), strtotime("2015-05-26 00:00")), 
     array('tennessee', strtotime("2015-10-29 00:00"), strtotime("2015-11-12 00:00")), 
     array('utah', strtotime("2015-09-15 00:00"), strtotime("2015-09-30 00:00")), 
     array('florida', strtotime("2015-11-12 00:01"), strtotime("2015-11-27 00:00")) 
    ); 

    function checkRange($location, $startDate, $endDate, $currentDate){ 
     if ($currentDate >= $startDate && $currentDate <= $endDate){ 
      // Successfully echos 'washington' to page 
      echo $location."<br>"; 
      return $location; 
     } 
    } 

    for ($i = 0; $i <= 6; $i++){ 
     checkRange($intervals[$i][0], $intervals[$i][1], $intervals[$i][2], $current); 
    } 
    //This does not echo the location from the checkRange function 
    echo checkRange(); 
} 

我只需要幫助讓promoDates函數返回checkRange函數正在迴應什麼。有沒有辦法可以在checkRange函數中設置一個變量並將其返回到promoDates函數中?

回答

0

那麼你的arent在任何參數傳遞給checkRange()功能...我也會改變checkRange功能的簽名採取的數組,間隔:

function promoDates() { 

    $current = strtotime("now"); 

    // Array gives a place, start date, and end date 
    $intervals = array(
     //The $current start time falls between the dates in washington array 
     array('washington', strtotime("2015-01-08 00:00"), strtotime("2015-01-30 00:00")), 
     array('california', strtotime("2015-06-02 00:00"), strtotime("2015-06-17 00:00")), 
     array('texas', strtotime("2015-02-12 00:00"), strtotime("2015-02-27 00:00")), 
     array('ney-york', strtotime("2015-05-12 00:00"), strtotime("2015-05-26 00:00")), 
     array('tennessee', strtotime("2015-10-29 00:00"), strtotime("2015-11-12 00:00")), 
     array('utah', strtotime("2015-09-15 00:00"), strtotime("2015-09-30 00:00")), 
     array('florida', strtotime("2015-11-12 00:01"), strtotime("2015-11-27 00:00")) 
    ); 

    // we will use this like a filter 
    function checkRange($currentDate, $promo) { 
     list($location, $startDate, $endDate) = $promo; 
     // return true or false 
     return (($currentDate >= $startDate) && ($currentDate <= $endDate)); 
    } 

    // loop over the intervals until we find a match then return it: 
    foreach($intervals as $promoData) { 
     if (checkRange($current, $promoData) === true) { 
      return $promoData[0]; 
     } 
    } 

    // return null if we do not find any 
    return null;  
} 

和使用看起來像:

echo promoDates(); 
+0

參數在for循環中傳遞。我實際上通過在循環中返回函數來修復它,但我也喜歡你的解決方案。 – jtpatton 2015-01-09 23:10:38

+0

其實我的修復程序並沒有完全起作用,但是你的修復程序完成了。非常感謝! – jtpatton 2015-01-10 00:56:26

0

編輯:我實際上只是通過遵循@prodigitalson的建議來修復它,並檢查每個循環迭代是真還是假,然後返回值爲真的地方。

function checkRange($startDate, $endDate, $currentDate, $loc){ 
    if ($currentDate >= $startDate && $currentDate <= $endDate){ 
     return true; 
    } else{ 
     return null; 
    } 
} 

for ($i = 0; $i <= 6; $i++){ 
    if(checkRange($intervals[$i][1], $intervals[$i][2], $current, $intervals[$i][0]) == true){ 
     return $intervals[$i][0]; 
    } 
} 

並刪除回聲checkRange();在底部。

+0

對不起,我剛剛刪除了我的答案,它是完整的垃圾,我應該更仔細地閱讀你的問題。但是您的解決方案也有一個缺陷:它只會檢查第一個位置並從checkRange返回值(無論是第一個位置的名稱還是NULL)。 – mmgross 2015-01-10 00:05:55

+0

我剛修好了^^感謝您提出這個建議! – jtpatton 2015-01-10 01:01:40