首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP如何从检查数组中多个日期范围的函数中获得值

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

Stack Overflow用户
提问于 2015-01-09 22:37:15
回答 2查看 328关注 0票数 0

我有一个函数来检查今天的日期是否在数组中的多个日期间隔之间。唯一需要的是这个函数返回checkRange函数中的checkRange值。

代码语言:javascript
复制
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函数中返回它?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-09 22:54:51

你不能把任何参数传递给checkRange()函数.我还将更改checkRange函数的签名,以获得间隔数组:

代码语言:javascript
复制
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;     
}

使用情况如下:

代码语言:javascript
复制
echo promoDates();
票数 0
EN

Stack Overflow用户

发布于 2015-01-09 23:07:50

编辑:我实际上只是通过遵循@prodigitalson建议并检查每个循环迭代是真还是假来修复它,然后返回它的值。

代码语言:javascript
复制
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()。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27870394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档