2012-06-13 41 views
14

ho la serie di date di seguitoGet data più recente da una serie di date

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

e vorrei conoscere la data più recente come: la più vicina alla data odierna.

Come posso farlo?

risposta

18

Fare un ciclo, convertire i valori fino alla data e archiviare il più recente, in una var.

$mostRecent= 0; 
foreach($dates as $date){ 
    $curDate = strtotime($date); 
    if ($curDate > $mostRecent) { 
    $mostRecent = $curDate; 
    } 
} 

una cosa del genere ... si ottiene l'idea Se si desidera più recente prima di oggi:

$mostRecent= 0; 
$now = time(); 
foreach($dates as $date){ 
    $curDate = strtotime($date); 
    if ($curDate > $mostRecent && $curDate < $now) { 
    $mostRecent = $curDate; 
    } 
} 
+1

Conversione date per timestamp in modo da avere int. quindi è un semplice confronto int, più grande = più recente. Se il tuo array è grande e cerchi performance, allora questo è probabilmente il modo più semplice e veloce. – PEM

+0

Ciao Pem, ho usato questo sistema e funziona perfettamente. Grazie Non provare gli altri, ma grazie a tutti ragazzi – sugarFornaciari

+0

Grazie, non dimenticare di convalidare la risposta :) grazie A proposito, se vuoi una data PEM

5

ordinare l'array in base alla data, e quindi ottenere il valore anteriore della matrice.

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

+1 Con una modifica, "più recente" non può essere una data futura o qualcosa del genere. $ Dates = array_filter ($ date, function ($ val) {return strtotime ($ val) mschr

0

Ecco la mia proposta:

$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

Quello è il mio variante. Funziona con data in futuro.

$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

Usa max(), array_map() e strtotime().

$max = max(array_map('strtotime', $arr)); 
echo date('Y-m-j H:i:s', $max); // 2012-06-11 08:30:49 
+5

Questa dovrebbe essere la risposta. Di gran lunga la soluzione più semplice rispetto al resto delle risposte. –

1

Credo che il seguente sia il codice più breve per trovare la data recente. puoi modificarlo per trovare l'indice della data recente o per trovare il recente in futuro o passato.

$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

Prova questo:

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

Rafael, SO è un sito di lingua inglese. Si prega di inviare risposte portoghesi per domande poste su http://pt.stackoverflow.com/. :) –

+1

La risposta restituisce sempre la data "massima", non il massimo delle date precedenti a oggi. Hai bisogno della seguente riga prima di aggiungere al tuo array $ newDates: if (strtotime ($ data)

+0

Ma "oggi" può essere perfettamente visto come "il data più recente, rispetto a oggi ". In questo caso non vedo alcun problema con la sua risposta. –

-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

Prova questa funziona al 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

Che bello, ma ti interessa spiegare cosa fa il tuo codice? – Machavity

+0

è già presente nella risposta di @ flowfree, penso – reverbnation

Problemi correlati