У меня есть функция, которая проверяет, попадает ли сегодня дата между несколькими интервалами дат в массиве. Единственное, что мне нужно, - это вернуть функцию $ location в функцию 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"))
);
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?
Ну, вы 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();
edit: Я на самом деле просто исправил его, следуя советам @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];
}
}
и удаление echo checkRange(); внизу.