Get most recent date from an array of dates

后端 未结 11 677
小鲜肉
小鲜肉 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;
      }
    }
    

提交回复
热议问题