Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

sort date file names by recent date

i have a list of file names
NewsletterMar12.pdf
NewsletterSep11.pdf
NewsletterMar11.pdf
NewsletterOct11.pdf
NewsletterSep10.pdf
NewsletterOct10.pdf
NewsletterNov11.pdf
NewsletterNov10.pdf
NewsletterJun11.pdf
NewsletterMay11.pdf
NewsletterMay10.pdf
NewsletterJun10.pdf
NewsletterMar10.pdf
NewsletterAug11.pdf
NewsletterJul11.pdf
NewsletterJul10.pdf
NewsletterJan12.pdf
NewsletterJan11.pdf
NewsletterFeb12.pdf
NewsletterJan10.pdf
NewsletterFeb11.pdf
NewsletterDec11.pdf
NewsletterFeb10.pdf
NewsletterDec10.pdf
NewsletterApr11.pdf
NewsletterApr10.pdf
NewsletterAug10.pdf

Open in new window

i assume we can take out Newsletter and .pdf some how
and sort by YY(year) and MMM(short month)
but how does one do this in php please

i tried to do this but so far i havent been able to do it
//NewsletterMar11.pdf	  
function date_sort_desc($a, $b)
{
  preg_match('/\w+\W?\d{4}/', $a, $matches_a);
  preg_match('/\w+\W?\d{4}/', $b, $matches_b);
  $timestamp_a = strtotime($matches_a[0]);
  $timestamp_b = strtotime($matches_b[0]);
  if ($timestamp_a == $timestamp_b) return 0;
  return $timestamp_a < $timestamp_b;
}

//usort($files, 'date_sort_desc');

usort( $files, create_function('$b, $a', 'return filemtime( $a ) - filemtime( $b );') );

Open in new window

i even tried on file time stamps too
im reading in the data from the file dir listing just fyi.
i figure it be better to sort my YY then MMM but i have no idea how to do that.

thank you in advance for any code or help you may provide
ASKER CERTIFIED SOLUTION
Avatar of Sudaraka Wijesinghe
Sudaraka Wijesinghe
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The file names are formatted wrong for date sorting.  You need to use the ISO-8601 format for something like this.  ISO-8601 goes from left-to-right with greater values (years) at the left, and lesser values (seconds) at the rightmost position.  All the components are numbers.  You can omit any of the positions at the right, so long as you preserve the positions at the left.  Example:

'NewsletterDec10.pdf' should be 'Newsletter2010-12.pdf'
This should get you going.  See http://www.laprbass.com/RAY_temp_pern.php where the file names are sorted in reverse chronological order.

See the list of sort algorithms here:
http://php.net/manual/en/array.sorting.php

HTH, ~Ray
<?php // RAY_temp_pern.php
error_reporting(E_ALL);
echo "<pre>";

// REQUIRED FOR PHP 5.1+
date_default_timezone_set('America/Chicago');

$list = <<<LIST
NewsletterMar12.pdf
NewsletterSep11.pdf
NewsletterMar11.pdf
NewsletterOct11.pdf
NewsletterSep10.pdf
NewsletterOct10.pdf
NewsletterNov11.pdf
NewsletterNov10.pdf
NewsletterJun11.pdf
NewsletterMay11.pdf
NewsletterMay10.pdf
NewsletterJun10.pdf
NewsletterMar10.pdf
NewsletterAug11.pdf
NewsletterJul11.pdf
NewsletterJul10.pdf
NewsletterJan12.pdf
NewsletterJan11.pdf
NewsletterFeb12.pdf
NewsletterJan10.pdf
NewsletterFeb11.pdf
NewsletterDec11.pdf
NewsletterFeb10.pdf
NewsletterDec10.pdf
NewsletterApr11.pdf
NewsletterApr10.pdf
NewsletterAug10.pdf
LIST;

// MAKE AN ARRAY
$arr = explode(PHP_EOL, $list);

// CONVERT THE DATES TO SOMETHING SENSIBLE FOR SORTING
foreach ($arr as $pdf)
{
    $str = str_replace('Newsletter', NULL, $pdf);
    $str = str_replace('.pdf',       NULL, $str);
    $str = substr($str,0,3) . ' 1, 20' . substr($str,3);
    $dat = date('c', strtotime($str));
    $out[$dat] = $pdf;
}

// ORDER THE LIST BY MOST RECENT
krsort($out);
print_r($out);

Open in new window

Avatar of Johnny

ASKER

@Ray_Paseur
you really need to make a few books, one for from scratch, not knowing a thing about php.
you always think of how to make things better and how they SHOULD be.

thanks for the help Ray_Paseur. Im going to go with sudaraka solution as his was first and used my code, although as i said i like yours too.

Thank you both im very grateful for the help.

@sudaraka
it looks like i sort of had it, just was missing a few more items of code and had the preg_match wrong too.i would of never got it with my code it looks like. but i seamed to of been on the right track. Thanks
Avatar of Johnny

ASKER

Supper fast replies thanks
@Pern Glad I could help. Good luck with your coding, and thanks for the points.