2012-06-13 173 views
14

数组日期最近我有日期的下方获取从日期

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

数组,并想知道最近的日期为:最接近今天的日期。

我该怎么做?

回答

18

做一个循环,将值转换为日期,并将最近的值存储在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; 
    } 
} 
+1

转换日期以时间戳,让你有整数。那么这是一个简单的int比较,更大=最近。如果你的阵列很大,并且你正在寻找表演,那么这可能是最简单和最快速的方式。 – PEM

+0

嗨Pem,我使用这个系统,它完美的作品。谢谢 还没有尝试别人,但感谢大家伙 – sugarFornaciari

+0

谢谢,不要忘记验证答案:)谢谢 顺便说一句,如果你想要一个日期<如今天,你可以简单地添加一个$ now = time();在循环之前,&& $ curDate <$现在在if条件中 – PEM

5

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

$dates = array(5) { /** omitted to keep code compact */ } 
$dates = array_combine($dates, array_map('strtotime', $dates)); 
arsort($dates); 
echo $dates[0]; 
+0

+1有了一个修改,'最近的'不能成为未来的日期或某个非常正确的.. $ dates = array_filter($日期,函数($ val){return strtotime( $ val) mschr

0

这里是我的建议:

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

那是我的变种。它在未来与日期一起工作。

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

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

这应该是答案。与其他答案相比,这是最简单的解决方案。 –

1

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

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

试试这个:

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

Rafael,SO是讲英语的网站。请在http://pt.stackoverflow.com/上发布关于问题的葡萄牙语答案。 :) –

+1

你的答案总是会返回'最大'日期,而不是最早的日期。在添加到$ newDates数组之前,您需要以下行:if(strtotime($ data)

+0

但是“today”可以完美地看作“最近的日期,与今天比较“。在这种情况下,我没有看到他的答案有任何问题。 –

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

试试这个工程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'; 
} 
1
$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))); 
+0

这很好,但是要小心解释你的代码做了什么? – Machavity

+0

我已经在@flowfree的答案中涵盖了它 – reverbnation