Last active
November 25, 2015 18:57
-
-
Save kodie/ddf112352a5ee59cf345 to your computer and use it in GitHub Desktop.
Sort an array with dates for keys by year descending and day ascending.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cmp($a, $b) { | |
$a = date('c', strtotime($a)); | |
$b = date('c', strtotime($b)); | |
$ay = date('Y', strtotime($a)); | |
$by = date('Y', strtotime($b)); | |
if ($a == $b) { | |
$r = 0; | |
} else { | |
if ($ay < $by) { | |
$r = 1; | |
} elseif ($ay > $by) { | |
$r = -1; | |
} else { | |
if ($a > $b) { | |
$r = 1; | |
} elseif ($a < $b) { | |
$r = -1; | |
} else { | |
$r = 0; | |
} | |
} | |
} | |
return $r; | |
} | |
uksort($array, 'cmp'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment