Php 从日期数组中获取最近的日期
我有下面的日期数组Php 从日期数组中获取最近的日期,php,arrays,date,date-comparison,Php,Arrays,Date,Date Comparison,我有下面的日期数组 array(5) { [0]=> string(19) "2012-06-11 08:30:49" [1]=> string(19) "2012-06-07 08:03:54" [2]=> string(19) "2012-05-26 23:04:04" [3]=> string(19) "2012-05-27 08:30:00" [4]=> string(19) "2012-06-08 08:3
array(5) {
[0]=> string(19) "2012-06-11 08:30:49"
[1]=> string(19) "2012-06-07 08:03:54"
[2]=> string(19) "2012-05-26 23:04:04"
[3]=> string(19) "2012-05-27 08:30:00"
[4]=> string(19) "2012-06-08 08:30:55"
}
并且想知道最近的日期,如:最接近今天的日期
我该怎么做呢?执行循环,将值转换为日期,并将最近的值存储在变量中
$mostRecent= 0;
foreach($dates as $date){
$curDate = strtotime($date);
if ($curDate > $mostRecent) {
$mostRecent = $curDate;
}
}
像这样的事。。。你明白了吗
如果您希望在今天之前获得最新信息:
$mostRecent= 0;
$now = time();
foreach($dates as $date){
$curDate = strtotime($date);
if ($curDate > $mostRecent && $curDate < $now) {
$mostRecent = $curDate;
}
}
$mostRecent=0;
$now=时间();
foreach($日期作为$日期){
$curDate=strottime($date);
如果($curDate>$mostRecent&$curDate<$now){
$mostRecent=$curDate;
}
}
按日期对数组排序,然后获取数组的前值
$dates = array(5) { /** omitted to keep code compact */ }
$dates = array_combine($dates, array_map('strtotime', $dates));
arsort($dates);
echo $dates[0];
以下是我的建议:
$most_recent = 0;
foreach($array as $key => $date){
if( strtotime($date) < strtotime('now') && strtotime($date) > strtotime($array[$most_recent]) ){
$most_recent = $key;
}
}
print $array[$most_recent]; //prints most recent day
$most\u recent=0;
foreach($key=>$date的数组){
if(strotime($date)strotime($array[$most_-recent])){
$most_recent=$key;
}
}
打印$array[$most_-recent]//打印最近的一天
$arrayy=array(
"2012-06-11 08:30:49","2012-06-07 08:03:54","2012-05-26 23:04:04",
"2012-05-27 08:30:00","2012-06-08 08:30:55"
);
函数getMostRecent($array){
$current=日期(“Y-m-d h:i:s”);
$diff1=NULL;
$recent=NULL;
foreach($数组作为$日期){
如果($diff=strcmp($current,$date)){
如果($diff1==NULL){
$diff1=$diff;
$recent=$date;
}
否则{
如果($diff<$diff1){
$diff1=$diff;
$recent=$date;
}
}
}
}
返回$recent;
}
这是我的变体。它与将来的日期一起工作
$Dates = array(
"2012-06-11 08:30:49",
"2012-06-07 08:03:54",
"2012-05-26 23:04:04",
"2012-05-27 08:30:00",
"2012-06-08 08:30:55",
"2012-06-12 07:45:45"
);
$CloseDate = array();
$TimeNow = time();
foreach ($Dates as $Date) {
$DateToCompare = strtotime($Date);
$Diff = $TimeNow - $DateToCompare;
if ($Diff < 0) $Diff *= -1;
if (count($CloseDate) == 0) {
$CloseDate['Date'] = $Date;
$CloseDate['Diff'] = $Diff;
continue;
}
if ($Diff < $CloseDate['Diff']) {
$CloseDate['Date'] = $Date;
$CloseDate['Diff'] = $Diff;
}
}
var_dump($CloseDate);
$Dates=数组(
"2012-06-11 08:30:49",
"2012-06-07 08:03:54",
"2012-05-26 23:04:04",
"2012-05-27 08:30:00",
"2012-06-08 08:30:55",
"2012-06-12 07:45:45"
);
$CloseDate=array();
$TimeNow=time();
foreach($日期作为$日期){
$DateToCompare=strottime($Date);
$Diff=$TimeNow-$DateToCompare;
如果($Diff<0)$Diff*=-1;
如果(计数($CloseDate)==0){
$CloseDate['Date']=$Date;
$CloseDate['Diff']=$Diff;
继续;
}
如果($Diff<$CloseDate['Diff'])){
$CloseDate['Date']=$Date;
$CloseDate['Diff']=$Diff;
}
}
var_dump($CloseDate);
使用和
我相信,下面是查找最近日期的最短代码。您可以修改它来查找最近日期的索引,或者查找最近的将来或过去的日期
$Dates = array(
"2012-06-11 08:30:49",
"2012-06-07 08:03:54",
"2012-05-26 23:04:04",
"2012-05-27 08:30:00",
"2012-06-08 08:30:55",
"2012-06-22 07:45:45"
);
$close_date = current($Dates);
foreach($Dates as $date)
if( abs(strtotime('now') - strtotime($date)) < abs(strtotime('now') - strtotime($close_date)))
$close_date = $date;
echo $close_date;
$Dates=数组(
"2012-06-11 08:30:49",
"2012-06-07 08:03:54",
"2012-05-26 23:04:04",
"2012-05-27 08:30:00",
"2012-06-08 08:30:55",
"2012-06-22 07:45:45"
);
$close_date=当前日期($Dates);
foreach($日期作为$日期)
如果(abs(strotime('now')-strotime('date))
试试这个:
public function getLargerDate(array $datas) {
$newDates = array();
foreach($datas as $data){
$newDates[strtotime($data)] = $data;
}
return $newDates[max(array_keys($newDates))];
}
试试这个方法100%有效
function getRecentDate($date_list,$curDate){
$curDate = strtotime($curDate);
$mostRecent = array();
foreach($date_list as $date){
$diff = strtotime($date)-$curDate;
if($diff>0){
$mostRecent[$diff] = $date;
}
}
if(!empty($mostRecent)){
ksort($mostRecent);
$mostRecent_key = key($mostRecent);
if($mostRecent_key){
return $mostRecent[$mostRecent_key];
}
}else{
return false;
}
}
$date_list = array('15-05-2015','14-01-2015','18-03-2015','20-10-2016','12-12-2014','12-12-2015');
$curDate = '14-01-2015';
$get_recent = getRecentDate($date_list,$curDate);
if($get_recent){
echo $get_recent;
}else{
echo 'No recent date exists';
}
将日期转换为时间戳,以便具有整数。然后是一个简单的整数比较,较大=最近。如果您的阵列很大,并且您正在寻找性能,那么这可能是最简单和最快的方法。嗨,Pem,我使用了这个系统,它工作得非常好。谢谢,我没有试过其他人,但是谢谢大家谢谢,别忘了验证答案:)顺便说一句,谢谢你,如果你想约会<就像今天一样,你可以简单地加上$now=time();在循环之前,&&$curDate<$now在if条件中我认为应该是$curDate=date('Ymd',strottime($date));不仅仅是$curDate=strottime($date)@IvorySantos我认为如果我们使用时间戳,我们不需要“日期('Ymd',strotime($date));”。使用TS比使用日期字符串imho+1更好,只需修改一次,“最近”不能是未来的日期或其他不太正确的内容$dates=array_filter($dates,function($val){return strotime($val)