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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题