如何从日期的数组获得最新的日期? [英] How to get most recent date from an array of dates?

查看:150
本文介绍了如何从日期的数组获得最新的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海兰家伙,我有日期的数组,如

Hy guys I have an array of dates such as

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" 
}

我想知道这是的最近的日期,相对于今天的日期

你有什么想法,这样做吗?

Do you have any idea to do that?

推荐答案

做一个循环,将值转换为日期,存储最新的,在一个变种。

Do a loop, convert the values to date, and store the most recent, in a var.

$mostRecent= 0;
foreach($dates as $date){
  $curDate = strtotime($date);
  if ($curDate > $mostRecent) {
     $mostRecent = $curDate;
  }
}

类似的东西...你的想法
如果你想要最新的今天之前:

something like that... you get the idea If you want most recent BEFORE today :

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

这篇关于如何从日期的数组获得最新的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆