2016-08-18 2 views
1

私は、タイムスタンプが多数ある配列を持っています。 は、私は最後の30分からのすべてのタイムスタンプを見つける必要があるタイムスタンプ範囲内のセットを見つける

現在のコード:

  //$history is a JSON decoded stream of arrays, of variable lengths 
      $hist = array(); 
      foreach($history as $newday){ 
        if($newday['Closed'] == $val){ 
          array_push($hist, $newday['Closed']); 
        } 
      } 
      var_dump($hist); 

サンプル配列データ:

array(24) { 
    [0]=> 
    string(22) "2016-08-18T05:24:40.47" 
    [1]=> 
    string(23) "2016-08-14T11:43:25.917" 
    [2]=> 
    string(23) "2016-08-16T08:26:39.693" 
    [3]=> 
    string(23) "2016-08-18T04:51:45.553" 

がどのように私は/(タイムスタンプの最後の30を見つける計算します$ hist)?

答えて

0

DateTimeクラスはこの種のものには最適です。

$timestamps = [ 
    "2016-08-18T05:24:40.47", 
    "2016-08-14T11:43:25.917", 
    "2016-08-16T08:26:39.693", 
    "2016-08-18T04:51:45.553", 
    "2016-08-18T16:30:45.553", 
]; 

$timeLimit = new DateTime('now'); 
$timeLimit->sub(new DateInterval('PT30M')); 

$recentTimestamps = []; 
foreach ($timestamps as $timestamp) { 
    $timestampDate = new DateTime($timestamp); 
    if ($timestampDate > $timeLimit) { 
     $recentTimestamps[] = $timestamp; 
    } 
} 

var_dump($recentTimestamps); 

出力:

array(1) { 
    [0] => 
    string(23) "2016-08-18T16:30:45.553" 
} 
0

はここにあなたにヒントを与える:

strtotime("2016-08-18T05:24:40.47") >= strtotime('-30minutes')

は、tryを持っています。

関連する問題