Get most recent date from an array of dates

sugarFornaciari picture sugarFornaciari · Jun 13, 2012 · Viewed 46.1k times · Source

I have the array of dates below

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.

How can I do that?

Answer

flowfree picture flowfree · Jun 13, 2012

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