Fine all goes perfectly, except that the sort field is based on file names from the directory contents, and they are (historically) ugly, as in MMDDYY, not YYYYMMDD, causing the different years to be interspersed together. I've read the man page at
http://us2.php.net/manual/en/function.sort.php, and quite can't grasp what to do here.
You probably want uksort().
Code:
function parseMDY($date) {
$month = substr($date, 0, 2);
$day = substr($date, 2, 2);
$year = substr($date, 4);
return array(
'm' => $month,
'd' => $day,
'y' => $year
);
}
function compareMDY($a, $b) {
if($a == $b)
return 0;
$da = parseMDY($a);
$db = parseMDY($b);
$ret = 0;
foreach(array('d', 'm', 'y') as $k)
if($da[$k] < $db[$k])
$ret = -1;
else if($da[$k] > $db[$k])
$ret = 1;
return $ret;
}
To sort an array on its keys by an MMDDYY or MMDDYYYY date, pass the latter of the above functions to uksort():
Code:
uksort($myArray, "compareMDY");
Bookmarks