从日期数组中获取最近的日期 [英] Get most recent date from an array of dates

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

问题描述

我有下面的日期数组

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

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

and would like to know the most recent date as in: the closest to today's date.

我该怎么做?

推荐答案

执行循环,将值转换为日期,并将最新的值存储在 var 中.

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天全站免登陆