doubijiao2094 2018-09-13 09:06
浏览 54
已采纳

PHP比较两个日期数组并检查匹配

I want have two arrays with date objects which I want to compare. The first array contains todays date and the next five days:

$today = date('Y-m-d');
$fiveDays = [];
for($i=0; $i <= 5; $i++){
    $today = date('Y-m-d', strtotime('+1 day', strtotime($today)));
    $fiveDays[] = date('Y-m-d', strtotime($today));
}

which gives the result:

0: "2018-09-14"
1: "2018-09-15"
2: "2018-09-16"
3: "2018-09-17"
4: "2018-09-18"
5: "2018-09-19"

the other array can contain multiple objects:

[{
   days: (4) ["2018-09-13", "2018-09-14", "2018-09-15", "2018-09-16"]
   duration: 4
   end: "2018-09-16"
   name: "vacation blabla"
   start: "2018-09-13"
},
{
   days: (5) ["2018-09-20", "2018-09-21", "2018-09-22", "2018-09-23", "2018-09-24"]
   duration: 5
   end: "2018-09-24"
   name: "vacation blabla"
   start: "2018-09-20"
}]

Now I want to check if one of the days/dates of the first array will or is in the vacations_array. How can I achieve that?

EDIT

When a match is found, the second array(with the vacation-periods) where the match is true, must be assigned to a $vacation-array

  • 写回答

3条回答 默认 最新

  • duankang5285 2018-09-13 10:12
    关注

    You can do it this way too:

    $result_array = array();
    $found = false;
    for($i=0; $i<count($vacation_array); $i++) {
      $found = false;
      for($j=0; $j<count($vacation_array[$i]['days']); $j++) {
        if(in_array($vacation_array[$i]['days'][$j], $array_1) && !$found) {
          $result_array[] = $vacation_array[$i];
          $found = true;
        }
      }
    }
    print_r($result_array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败