kodie
7/30/2015 - 6:27 PM

Sort an array with dates for keys by year descending and day ascending.

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');