简体   繁体   English

PHP如何从检查数组中多个日期范围的函数中获取值

[英]PHP How to get value from function that checks between multiple date ranges in array

I have a function that checks if today's date falls in between multiple date intervals in an array. 我有一个检查今天的日期是否在数组中多个日期间隔之间的函数。 The only thing I need is for this function to return the $location value in the checkRange function. 我唯一需要的是此函数在checkRange函数中返回$ location值。

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();
}

I just need help getting the promoDates function to return what the checkRange function is echoing. 我只需要获取promoDates函数来返回checkRange函数正在回显的内容的帮助。 Is there a way I can set a variable inside the checkRange function and return it in the promoDates function? 有什么方法可以在checkRange函数中设置变量并将其返回到promoDates函数中吗?

Well you arent passing in any arguments to the checkRange() function... I would also change the signature of the checkRange function to take an array for interval: 好吧,您没有将任何参数传递给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;     
}

And usage would look like: 用法如下所示:

echo promoDates();

edit: I actually just fixed it by following @prodigitalson advice and checking if each loop iteration is true or false, then returning the value where it is true. 编辑:实际上,我只是按照@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];
    }
}

and removing the echo checkRange(); 并删除echo echoRange(); at the bottom. 在底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM