加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 从日期数组获取最近的日期

发布时间:2020-12-13 13:43:21 所属栏目:PHP教程 来源:网络整理
导读:我有下面的数组 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" } 并且想知道最近的日期
我有下面的数组
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" 
}

并且想知道最近的日期如:最接近今天的日期.

我怎样才能做到这一点?

执行循环,将值转换为日期,并将最近的值存储在var中.
$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;
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读