Skip to content

Instantly share code, notes, and snippets.

@kodie
Last active November 25, 2015 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kodie/ddf112352a5ee59cf345 to your computer and use it in GitHub Desktop.
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.
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