Solved

sort date file names by recent date

Posted on 2012-03-25
6
358 Views
Last Modified: 2012-03-26
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
0
Comment
Question by:Johnny
  • 2
  • 2
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
Sudaraka Wijesinghe earned 500 total points
ID: 37763616
Try this.
(Please note the Y2K bug)
$list = array(
	'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',
);

print_r($list);

usort($list, 'date_sort_desc');

print_r($list);

function date_sort_desc($a, $b) {
	if(0 > preg_match('/^Newsletter([a-zA-Z]{3})(\d{2})\.pdf$/', $a, $match_a)) return 0;
	if(0 > preg_match('/^Newsletter([a-zA-Z]{3})(\d{2})\.pdf$/', $b, $match_b)) return 0;

	$date_a = strtotime('20' . $match_a[2] . '-' . $match_a[1] . '-01');
	$date_b = strtotime('20' . $match_b[2] . '-' . $match_b[1] . '-01');

	if($date_a == $date_b) return 0;

	return ($date_a < $date_b);
}

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37765382
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'
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37765426
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Johnny
ID: 37765780
@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
0
 

Author Closing Comment

by:Johnny
ID: 37765788
Supper fast replies thanks
0
 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
ID: 37766615
@Pern Glad I could help. Good luck with your coding, and thanks for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now