Get most recent date from an array of dates

后端 未结 11 619
小鲜肉
小鲜肉 2020-11-28 11:32

I have the array of dates below

array(5) { 
    [0]=> string(19) \"2012-06-11 08:30:49\" 
    [1]=> string(19) \"2012-06-07 08:03:54\" 
    [2]=> st         


        
相关标签:
11条回答
  • 2020-11-28 11:32

    Do a loop, convert the values to date, and store the most recent, in a var.

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

    something like that... you get the idea If you want most recent BEFORE today :

    $mostRecent= 0;
    $now = time();
    foreach($dates as $date){
      $curDate = strtotime($date);
      if ($curDate > $mostRecent && $curDate < $now) {
         $mostRecent = $curDate;
      }
    }
    
    0 讨论(0)
  • 2020-11-28 11:34

    Here is my suggestion:

    $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
    
    0 讨论(0)
  • 2020-11-28 11:35

    Thats my variant. It works with date in future.

    $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);
    
    0 讨论(0)
  • 2020-11-28 11:41
    $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;
    }
    
    0 讨论(0)
  • 2020-11-28 11:41

    Try this:

    public function getLargerDate(array $datas) {
        $newDates = array();
        foreach($datas as $data){
            $newDates[strtotime($data)] = $data;
        }
        return $newDates[max(array_keys($newDates))];
    }
    
    0 讨论(0)
  • 2020-11-28 11:42

    Try this works 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';
    }
    
    0 讨论(0)
提交回复
热议问题