我有一个包含这些值的数组并且必须找到这些中最新的
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
)
)
从现在到现在,我只需要带一个身份证——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
。