简体   繁体   中英

find the most recent value between some date in php

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM