简体   繁体   English

在PHP中找到某个日期之间的最新值

[英]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. 从现在到现在,我只需要输入一个ID(1小时)。

EDIT : THIS IS A SOLUTION THANKS TO @evan-taylor 编辑: 这是@ 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 . 查看函数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. 尚未测试此代码,但$most_recent_id应包含不超过1小时前的最新时间戳的ID;如果不存在,则为NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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