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

前端之家收集整理的这篇文章主要介绍了php – 从日期数组获取最近的日期前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有下面的数组 @H_404_1@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中. @H_404_1@$mostRecent= 0; foreach($dates as $date){ $curDate = strtotime($date); if ($curDate > $mostRecent) { $mostRecent = $curDate; } }

这样的事情…你会得到这个想法
如果您希望今天以前最近:

@H_404_1@$mostRecent= 0; $now = time(); foreach($dates as $date){ $curDate = strtotime($date); if ($curDate > $mostRecent && $curDate < $now) { $mostRecent = $curDate; } }

猜你在找的PHP相关文章