简体   繁体   中英

PHP Search in array for value between two given dates

I have this array:

Array
(
    [1] => Array
        (
            ['s_date'] => 1/1/1989
            ['e_date'] => 6/30/1989
            ['rate'] => 7.92
        )

    [2] => Array
        (
            ['s_date'] => 7/1/1989
            ['e_date'] => 12/31/1989
            ['rate'] => 8.18
        )

    [3] => Array
        (
            ['s_date'] => 1/1/1990
            ['e_date'] => 6/30/1990
            ['rate'] => 7.14
        )

So I want to search for rate between two given dates but I am not sure how to go about with that, i looked into array_search function but it didn't help.

function findRate($startDate, $endDate) {
  // not sure here what needs to be done...
}

Thanks for the help

Only to give you a direction, you question is a little unclear, when it comes to your function declaration. Why 2 dates for the lookup?

So i did this

function findRate($date,$array){
 $rate=null;
 foreach($yourArray as $value){
  $s=date('Y-m-d',strtotime($value['s_date']));
  $e=date('Y-m-d',strtotime($value['e_date']));
  if($s<=$lookup && $lookup<=$e){
    $result = $value['rate'];
  }
 }
 return $rate;
}

$rate = findRate('1989-02-02',$yourarrayfromabove);//Result  8.18

Try to understand everthing i do here and extent and change if needed.

And open the next question whit self written code, thnx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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