web-dev-qa-db-ja.com

日付の配列から最新の日付を取得する

以下の日付の配列があります

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

最新の日付のように:今日の日付に最も近い日付を知りたい。

どうやってやるの?

23
sugarFornaciari

ループを実行して、値を日付に変換し、最新のものを変数に格納します。

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

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
74
flowfree

配列を日付で並べ替え、配列の前の値を取得します。

$dates = array(5) { /** omitted to keep code compact */ }
$dates = array_combine($dates, array_map('strtotime', $dates));
arsort($dates);
echo $dates[0];
6
FThompson
$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)));
4
sumeet

最近の日付を見つけるための最短のコードは次のとおりです。これを変更して、最近の日付のインデックスを見つけたり、将来または過去の最近のものを見つけたりすることができます。

$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;
2
FatalError

それは私の亜種です。将来的には日付で動作します。

$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);
2
AlexeyKa

これを試して:

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

これが私の提案です:

$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
vinculis
$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
Miqdad Ali

これを試すと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
Anwar Hussain