Solved

sort date file names by recent date

Posted on 2012-03-25
6
355 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

12 Experts available now in Live!

Get 1:1 Help Now