Solved

sort date file names by recent date

Posted on 2012-03-25
6
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
integrated two Separate file . 3 30
Conditional Array Element 7 28
Why is my select returning NaN 23 46
Doubt with angularJs with PHP 4 22
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

752 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