Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sort date file names by recent date

Posted on 2012-03-25
6
Medium Priority
?
367 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 2000 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 111

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 111

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 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