0

I have an array with this values ​​and have to find the most recent of these

Array (  
    [0] => stdClass Object ( 
        [createdDate] => 2016/03/30 22:27:26:000 
        [createdDateUTC] => 2016-03-30T21:27:26 
        [id]=>1
    ), 
    [1] => stdClass Object ( 
        [createdDate] => 2016/03/30 22:27:26:000 
        [createdDateUTC] => 2016-03-30T21:27:26
        [id]=>2 
    )
)

I need to take only an id between now and now - 1 hour.

EDIT : THIS IS A SOLUTION THANKS TO @evan-taylor

$dates_arr=array(  
    array( 
        'createdDate' => '2016/03/30 22:27:26:000', 
        'createdDateUTC' => '2016-03-30T20:27:26',
        'id'=>1
    ), 
    array( 
        'createdDate' => '2016/03/30 22:27:26:000', 
        'createdDateUTC' => '2016-03-30T21:27:26',
        'id'=>2 
    )
);
$most_recent_time = 0;
$most_recent_id = NULL;
foreach($dates_arr as $date_obj){
    $parsed_time = strtotime($date_obj['createdDateUTC']);
    if($parsed_time > $most_recent_time && ($parsed_time >= (time() - 3600))){
        $most_recent_time = $parsed_time;
        $most_recent_id = $date_obj['id'];
    }
}
echo $most_recent_id;
colapiombo
  • 179
  • 1
  • 3
  • 13
  • 1
    Possible duplicate: http://stackoverflow.com/questions/11012891/how-to-get-most-recent-date-from-an-array-of-dates – Jon Mar 30 '16 at 22:26

1 Answers1

1

Something like this would probably work for you. Look into the function strtotime.

$most_recent_time = 0;
$most_recent_id = NULL;
foreach($dates_arr as $date_obj){
    $parsed_time = strtotime($date_obj->createdDateUTC);
    if($parsed_time > $most_recent_time && ($parsed_time >= (time() - 3600)){
        $most_recent_time = $parsed_time
        $most_recent_id = $date_obj->id;
    }
}

Haven't tested this code but $most_recent_id should contain the id of the most recent timestamp that is no more than 1 hour ago or NULL if none exist.

Evan Taylor
  • 216
  • 3
  • 9