试试这个工程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';
}
转换日期以时间戳,让你有整数。那么这是一个简单的int比较,更大=最近。如果你的阵列很大,并且你正在寻找表演,那么这可能是最简单和最快速的方式。 – PEM
嗨Pem,我使用这个系统,它完美的作品。谢谢 还没有尝试别人,但感谢大家伙 – sugarFornaciari
谢谢,不要忘记验证答案:)谢谢 顺便说一句,如果你想要一个日期<如今天,你可以简单地添加一个$ now = time();在循环之前,&& $ curDate <$现在在if条件中 – PEM