【问题标题】:Get most recent date from an array of dates in "Y-m-d H:i:s" format从“Y-m-d H:i:s”格式的日期数组中获取最新日期
【发布时间】:2012-06-16 07:02:32
【问题描述】:

我有 Y-m-d H:i:s 格式的日期数组,例如:

array(5) { 
    [0]=> string(19) "2012-06-11 08:30:49" 
    [1]=> string(19) "2012-06-07 08:03:54" 
    [2]=> string(19) "2012-05-26 23:04:04" 
    [3]=> string(19) "2012-05-27 08:30:00" 
    [4]=> string(19) "2012-06-08 08:30:55" 
}

我想知道最近的日期

换句话说,今天是 2012 年 6 月 13 日,哪个日期时间最接近今天的日期?

从我的示例数组中,我期待 2012-06-11 08:30:49

我该怎么做?

【问题讨论】:

  • 最近的日期如:最接近今天的日期”到底是什么意思?您想要不超过当前日期的最近日期吗?这只是您对英语选择的误解吗?您之前的所有日期都早于您发布此问题的日期。

标签: php arrays date max date-comparison


【解决方案1】:

经过近 10 年和近 50,000 次浏览量,似乎没有人能从树上看到森林。

日期时间戳数组的格式非常适合简单的字符串比较。除了max() 之外,绝对没有理由调用任何东西来获取最高/最新的字符串。您的Y-m-d H:i:s 格式化值可以作为字符串进行比较,因为单位整数的排列顺序是从最大到最小。所有整数都始终用零填充。几乎不需要任何准备。

代码:(Demo)

$dates = [
    "2012-06-11 08:30:49",
    "2012-06-07 08:03:54",
    "2012-05-26 23:04:04",
    "2012-05-27 08:30:00",
    "2012-06-08 08:30:55",
];

echo max($dates);
// 2012-06-11 08:30:49

【讨论】:

    【解决方案2】:
    $dates = [
        "2012-06-11 08:30:49" 
        ,"2012-06-07 08:03:54" 
        ,"2012-05-26 23:04:04" 
        ,"2012-05-27 08:30:00" 
        ,"2012-06-08 08:30:55" 
    ];
    echo date("Y-m-d g:i:s",max(array_map('strtotime',$dates)));
    

    【讨论】:

    • 这很好,但请解释一下您的代码是做什么的?
    【解决方案3】:

    100% 试用此方法

    function getRecentDate($date_list,$curDate){
    $curDate = strtotime($curDate); 
        $mostRecent = array();
        foreach($date_list as $date){                                             
           $diff = strtotime($date)-$curDate;
           if($diff>0){
            $mostRecent[$diff] = $date;
           }
        }   
        if(!empty($mostRecent)){
            ksort($mostRecent);            
            $mostRecent_key = key($mostRecent);
            if($mostRecent_key){
                return $mostRecent[$mostRecent_key];
            }
        }else{
            return false;
        }
    }
    $date_list = array('15-05-2015','14-01-2015','18-03-2015','20-10-2016','12-12-2014','12-12-2015');
    $curDate = '14-01-2015';    
    $get_recent = getRecentDate($date_list,$curDate);
    if($get_recent){
        echo $get_recent;
    }else{
        echo 'No recent date exists';
    }
    

    【讨论】:

      【解决方案4】:
      $DatesList = array( '2015-05-19', '2015-09-17', '2015-09-24', '2015-10-02', '2015-10-23', '2015-11-12', '2015-12-25' );
      
      $counter = 0; 
      $currentDate = date("Y-m-d");
      foreach ($DatesList as $dates)
      {
          if($dates >= $currentDate)
          {
              $storeDates[$counter] = $dates;
              $counter++;
          }
      }
      $closestDate = current($storeDates);
      echo $closestDate;
      

      【讨论】:

        【解决方案5】:

        试试这个:

        public function getLargerDate(array $datas) {
            $newDates = array();
            foreach($datas as $data){
                $newDates[strtotime($data)] = $data;
            }
            return $newDates[max(array_keys($newDates))];
        }
        

        【讨论】:

        • Rafael, SO 是一个英语网站。请在pt.stackoverflow.com 发布葡萄牙语答案。 :)
        • 您的答案总是返回“最大”日期,而不是早于今天的最大日期。在添加到 $newDates 数组之前,您需要以下行: if(strtotime($data)
        • 但是“今天”可以完美地视为“最近的日期,与今天相比”。在这种情况下,我认为他的回答没有任何问题。
        【解决方案6】:

        执行循环,将值转换为日期,并将最新的值存储在 var 中。

        $mostRecent= 0;
        foreach($dates as $date){
          $curDate = strtotime($date);
          if ($curDate > $mostRecent) {
             $mostRecent = $curDate;
          }
        }
        

        类似的东西......你明白了 如果你想要今天之前的最新消息:

        $mostRecent= 0;
        $now = time();
        foreach($dates as $date){
          $curDate = strtotime($date);
          if ($curDate > $mostRecent && $curDate < $now) {
             $mostRecent = $curDate;
          }
        }
        

        【讨论】:

        • 将日期转换为时间戳,以便您拥有整数。然后这是一个简单的 int 比较,更大 = 最近。如果您的阵列很大,并且您正在寻找性能,那么这可能是最简单,最快的方法。
        • 谢谢,别忘了验证答案 :) 谢谢 顺便说一句,如果你想要一个日期
        • 我认为应该是 $curDate = date('Ymd',strtotime($date));而不仅仅是 $curDate = strtotime($date);
        • @IvorySantos 我认为我们不需要“date('Ymd',strtotime($date));”如果我们使用时间戳。使用 TS 比使用日期字符串更好
        【解决方案7】:

        我相信,以下是查找最近日期的最短代码。您可以更改它以查找最近日期的索引或查找最近的未来或过去。

        $Dates = array( 
        "2012-06-11 08:30:49", 
        "2012-06-07 08:03:54", 
        "2012-05-26 23:04:04",
        "2012-05-27 08:30:00",
        "2012-06-08 08:30:55",
        "2012-06-22 07:45:45"
        );
        
        $close_date = current($Dates);
        foreach($Dates as $date)
            if( abs(strtotime('now') - strtotime($date)) < abs(strtotime('now') - strtotime($close_date)))
                $close_date = $date;
        
        echo $close_date;
        

        【讨论】:

          【解决方案8】:

          使用max()array_map()strtotime()

          $max = max(array_map('strtotime', $arr));
          echo date('Y-m-j H:i:s', $max); // 2012-06-11 08:30:49
          

          【讨论】:

            【解决方案9】:

            那是我的变种。它适用于未来的日期。

            $Dates = array( 
                "2012-06-11 08:30:49", 
                "2012-06-07 08:03:54", 
                "2012-05-26 23:04:04",
                "2012-05-27 08:30:00",
                "2012-06-08 08:30:55",
                "2012-06-12 07:45:45"
            );
            $CloseDate = array();
            $TimeNow = time();
            foreach ($Dates as $Date) {
              $DateToCompare = strtotime($Date);
              $Diff = $TimeNow - $DateToCompare;
              if ($Diff < 0) $Diff *= -1;
              if (count($CloseDate) == 0) {
                $CloseDate['Date'] = $Date;
                $CloseDate['Diff'] = $Diff;
                continue;
              }
              if ($Diff < $CloseDate['Diff']) {
                $CloseDate['Date'] = $Date;
                $CloseDate['Diff'] = $Diff;
              }
            }
            
            var_dump($CloseDate);
            

            【讨论】:

              【解决方案10】:
              $arrayy = array(
                  "2012-06-11 08:30:49","2012-06-07 08:03:54","2012-05-26 23:04:04",
                  "2012-05-27 08:30:00","2012-06-08 08:30:55" 
              );
              
              function getMostRecent($array){
                  $current = date("Y-m-d h:i:s");
                  $diff1 = NULL;
                  $recent = NULL;
                  foreach($array as $date){
                      if($diff = strcmp($current,$date)){
                          if($diff1 == NULL){
                              $diff1 = $diff;
                              $recent = $date;
                          }
                          else{
                              if($diff < $diff1){
                                  $diff1 = $diff;
                                  $recent = $date;
                              }
                          }
                      }
                  }
                  return $recent;
              }
              

              【讨论】:

                【解决方案11】:

                这是我的建议:

                $most_recent = 0;
                
                foreach($array as $key => $date){
                    if( strtotime($date) < strtotime('now') && strtotime($date) > strtotime($array[$most_recent]) ){
                        $most_recent = $key;
                    }
                }
                
                print $array[$most_recent]; //prints most recent day
                

                【讨论】:

                  【解决方案12】:

                  按日期对数组进行排序,然后得到数组的前值。

                  $dates = array(5) { /** omitted to keep code compact */ }
                  $dates = array_combine($dates, array_map('strtotime', $dates));
                  arsort($dates);
                  echo $dates[0];
                  

                  【讨论】:

                  • +1 进行一次修改后,“最近的”不能是未来的日期或不太正确的东西.. $dates = array_filter($dates, function($val) { return strtotime($val)
                  • 不需要日期,只需排序并获得最高值:echo end(asort($dates));
                  猜你喜欢
                  • 2010-12-23
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-05-12
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-07-28
                  相关资源
                  最近更新 更多