从一个以"Y-m-d H:i:s"格式表示的日期数组中获取最近的日期

30

我有一个日期数组,格式为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

我该怎么做?


“_most recent date as in: the closest to today's date_”指的是什么?您想要最近的日期,但不超过当前日期吗?这只是您选择英语的误解吗?所有的日期都早于您发布此问题的日期。 - mickmackusa
12个回答

96

使用 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

33

使用循环,将值转换为日期,并将最新的日期存储在变量中。

$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;
  }
}

1
将日期转换为时间戳,以便您获得整数。然后进行简单的整数比较,大的表示最近的日期。如果您的数组很大,并且您正在寻找性能,则这可能是最简单和最快速的方法。 - PEM
谢谢,别忘了验证答案 :) 谢谢 顺便说一下,如果你想要一个小于今天的日期,你可以在循环之前添加 $now = time(); ,并且在 if 条件中加上 && $curDate < $now - PEM
我认为应该是 $curDate = date('Ymd',strtotime($date)); 而不仅仅是 $curDate = strtotime($date); - Sam San
1
@IvorySantos 我认为如果我们使用时间戳,就不需要“date('Ymd',strtotime($date));”了。在我看来,使用时间戳比使用日期字符串更好。 - PEM

6
$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)));

1
很好,但能解释一下你的代码是做什么的吗? - Machavity

6
按日期对数组进行排序,然后获取数组的第一个值。
$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) < time(); }); - mschr
返回翻译的文本:无需日期,仅排序并获取最高值:回显end(asort($ dates)); - Jonathan Joosten

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);

1
请尝试这个,百分之百有效。
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';
}

1

试试这个:

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

Rafael,SO是一个英语网站。请在http://pt.stackoverflow.com/上发布葡萄牙语答案以回答问题。 :) - Bruno Toffolo
1
你的答案总是返回“最大”的日期,而不是早于今天的日期中的最大日期。在将数据添加到 $newDates 数组之前,您需要添加以下行:if(strtotime($data) < strtotime(date('Y-m-j H:i:s'))){ - Warren Sergent
但是,“今天”可以被视为“与今天相比最近的日期”。在这种情况下,我认为他的回答没有任何问题。 - Bruno Toffolo

1
在近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

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

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接