【问题标题】:Remove from array past dates从数组中删除过去的日期
【发布时间】:2021-07-23 21:54:30
【问题描述】:

我有以下数组,不同年份的不同混合日期

       Array
    (
        [0] => 2016-05-18
        [1] => 2016-06-18
        [2] => 2016-08-13
        [3] => 2016-09-03
        [4] => 2016-10-08
        [5] => 2016-08-06
        [6] => 2016-09-30
        [7] => 2016-09-10
        [8] => 2016-07-09
        [9] => 2016-06-13
        [10] => 2016-06-15
        [11] => 2016-07-30
        [12] => 2016-08-27
        [13] => 2016-07-02
        [14] => 2016-11-01
        [15] => 2016-09-18
        [16] => 2016-11-06
        [17] => 2016-11-07
        [18] => 2017-06-17
        [19] => 2017-06-22
        [20] => 2017-06-21
        [21] => 2017-10-01
        [22] => 2017-07-08
        [23] => 2017-05-27
        [24] => 2017-06-06
        [25] => 2017-09-09
        [26] => 2017-04-16
        [27] => 2017-09-16
        [28] => 2017-07-29
        [29] => 2017-08-05
        [30] => 2017-09-03
        [31] => 2017-06-24
        [32] => 2017-08-26
        [33] => 2017-07-22
        [34] => 2018-05-28
        [35] => 2018-06-09
        [36] => 2017-10-16
        [37] => 2017-10-28
        [38] => 2017-10-08
        [39] => 2017-11-04
        [40] => 2018-06-20
        [41] => 2018-08-05
        [42] => 2018-09-03
        [43] => 2018-06-16
        [44] => 2018-03-31
        [45] => 2019-05-25
        [46] => 2021-05-25
        [47] => 2021-05-26
        [48] => 2021-05-27
)

我想删除早于 2021 年的所有日期,这样我的新数组将只包含属于当年 2021 年的日期。如何过滤新数组以使 2021 年之前的所有日期都消失?

 Array
    (
        [0] => 2021-05-25
        [1] => 2021-05-26
        [2] => 2021-05-27
  )

【问题讨论】:

    标签: php arrays array-filter


    【解决方案1】:

    您可以创建一个循环遍历数组的函数,并检查字符串是否带有 2021 如果不是,则删除该元素。

    对于 php 8,您可以使用以下功能: str_starts_with(string $haystack, string $needle): bool

    例如:

    foreach($array as $key => $val){
         if(!str_starts_with('2021', $val)){
              unset($array[$key]);
         }
    }
    

    对于以前的 php 版本,您可以使用 strpos 示例:

    foreach($array as $key => $val){
         if (!strpos($val, '2021') == 0) {
              unset($array[$key]);
         }
    }
    

    【讨论】:

      【解决方案2】:

      您可以遍历每个日期并使用 dateTime 对象比较时间,以查看该日期是否已超过设定日期(如年初),如果尚未超过,则将其添加到新数组中。见以下代码:

      <?php
      //array with mixed date data
      $dateArray = array("2021-01-01","2016-05-18","2017-09-03","2018-06-16","2019-05-25","2021-05-25","2021-05-26","2021-05-27");
      
      //the day you want to have everyday after (in this case the start of 2021)
      $setDate = "2021-01-01";
      
      //array where the dates greater than then the set date will be stored.
      $newTimesArray = array();
      
      foreach ($dateArray as $date) {
          $given_date = new \DateTime($date); //convert string to a date time object
          if ($given_date >= new \DateTime($setDate)) { //if the date is greater than or equal to "2021-01-01" add it to the new array
                $newTimesArray[]=$date;
              }
      }
      
      //print the results
      echo print_r($newTimesArray,true);
       ?>
      

      【讨论】:

        【解决方案3】:

        你有 hashtag array-filter 并且 PHP 有 array_filter 函数:

        $dates = [
            '2016-05-18',
            '2018-03-31',
            '2019-05-25',
            '2021-05-25',
            '2021-05-26',
            '2021-05-27',
        ];
        
        function myFilter($dt) {
            $year = date('Y',strtotime($dt));
            return $year > 2020;
        }
        print_r(array_values(array_filter($dates, 'myFilter')));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-15
          • 1970-01-01
          • 2014-01-31
          • 1970-01-01
          • 2011-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多