Help with PHP date sorting...

Associate
Joined
18 Oct 2002
Posts
2,092
Location
Edinburgh
Im currently trying to sort my latest uploads by date, the method i have come up with seems very complex and im sure there must be an easier way of doing it.

This is what im displaying at the moment.
http://www.mattinglis.com/index.php?Pagename=Upload

I want to eventually sort the current year by month aswell but just the current year.

This is the complex code im using also note its not future proof which i would like.

Code:
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_DESC,
$files
);

echo '<font size=+1><b>2009</b><br><br></font>';
foreach ($files as $filename) {
    $datesort = filemtime($filename);
    if (date('Y', $datesort) == "2009"){
    $sizeoffile = (filesize($filename)/1024);
    $dateoffile = date("d/m/Y",filemtime($filename));
    echo '<a href="http://www.mattinglis.com/'.$filename.'">'.$filename.'</a>   ---                <b>Filesize</b> :  '.number_format($sizeoffile, 2, '.', '').' KBytes  -----         <b>Modified</b> : '.$dateoffile.'<br>';
   }
}
....
//and so on
....


Can anyone help?
 
untested but what you'd do is put the year header inside the loop and only output it when the year changes. we save the current year at the end of each loop so we can check on the next.

PHP:
foreach ($files as $filename) {
  $current_year = date('Y', filemtime($filename));
  if($current_year != $previous_year) echo '<font size=+1><b>' . $current_year . '</b><br><br></font>';
  $sizeoffile = (filesize($filename)/1024);
  $dateoffile = date("d/m/Y",filemtime($filename));
  echo '<a href="http://www.mattinglis.com/'.$filename.'">'.$filename.'</a>   ---                <b>Filesize</b> :  '.number_format($sizeoffile, 2, '.', '').' KBytes  -----         <b>Modified</b> : '.$dateoffile.'<br>';
  $previous_year = $current_year;
}
 
Back
Top Bottom