Get most recent date from an array of dates
Use max()
, array_map()
, and strtotime()
.
$max = max(array_map('strtotime', $arr));echo date('Y-m-j H:i:s', $max); // 2012-06-11 08:30:49
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 ideaIf you want most recent BEFORE today :
$mostRecent= 0;$now = time();foreach($dates as $date){ $curDate = strtotime($date); if ($curDate > $mostRecent && $curDate < $now) { $mostRecent = $curDate; }}
Sort the array by date, and then get the front value of the array.
$dates = array(5) { /** omitted to keep code compact */ }$dates = array_combine($dates, array_map('strtotime', $dates));arsort($dates);echo $dates[0];