28

我有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.

我怎样才能做到这一点?

4

12 回答 12

91

使用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
于 2012-06-13T11:01:18.607 回答
32

执行循环,将值转换为日期,并将最新的值存储在 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;
  }
}
于 2012-06-13T10:29:25.250 回答
6

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

$dates = array(5) { /** omitted to keep code compact */ }
$dates = array_combine($dates, array_map('strtotime', $dates));
arsort($dates);
echo $dates[0];
于 2012-06-13T10:29:28.967 回答
5
$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)));
于 2016-05-02T14:22:07.623 回答
2

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

$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);
于 2012-06-13T10:35:13.243 回答
2

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

$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;
于 2012-06-13T11:15:11.670 回答
1

这是我的建议:

$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
于 2012-06-13T10:29:36.957 回答
1
$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;
}
于 2012-06-13T10:31:32.907 回答
1

试试这个:

public function getLargerDate(array $datas) {
    $newDates = array();
    foreach($datas as $data){
        $newDates[strtotime($data)] = $data;
    }
    return $newDates[max(array_keys($newDates))];
}
于 2015-04-08T18:54:24.167 回答
1

试试这个作品 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';
}
于 2015-10-31T06:45:13.773 回答
0

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

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

代码:(演示

$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
于 2022-01-21T08:46:48.120 回答
-1
$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;
于 2015-05-19T08:44:01.870 回答