我有一個函數,用於檢查當前日期是否落在數組中的多個日期區間之間。我唯一需要的是這個函數返回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函數中?
參數在for循環中傳遞。我實際上通過在循環中返回函數來修復它,但我也喜歡你的解決方案。 – jtpatton 2015-01-09 23:10:38
其實我的修復程序並沒有完全起作用,但是你的修復程序完成了。非常感謝! – jtpatton 2015-01-10 00:56:26