簡體   English   中英

在PHP中找到某個日期之間的最新值

[英]find the most recent value between some date in php

我有一個具有此值的數組,並且必須找到這些值中的最新值

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 
    )
)

從現在到現在,我只需要輸入一個ID(1小時)。

編輯: 這是@ 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;

這樣的事情可能會為您工作。 查看函數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;
    }
}

尚未測試此代碼,但$most_recent_id應包含不超過1小時前的最新時間戳的ID;如果不存在,則為NULL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM