user867621
user867621

Reputation: 1197

Sort an array of dates formatted as \i\mmdY

I have a bunch of files that are in an array

im(month)(day)(year)

im01012007

im01022006

im01022012

im01032011

im01042010

im01042012

im01052009

im01052011

im01062012

im01072008

im01072011

etc..

is there a way to sort the array so that it sorted by year then month then day?

Upvotes: 1

Views: 259

Answers (5)

Sonny
Sonny

Reputation: 8326

This will sort oldest to newest. Use krsort to sort newest to oldest.

$files = array();
if ($handle = opendir('/path/to/files')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            $y = substr($entry, 6, 4);
            $m = substr($entry, 2, 2);
            $d = substr($entry, 4, 2);
            $files[$y . $m . $d] = $entry;
        }
    }
    closedir($handle);
}
ksort($files);

I used these PHP Manual entries to write this solution:

Upvotes: 1

Baba
Baba

Reputation: 95101

You can try use usort

$string = "im01012007.jpg,im01022006.jpg,im01022012.jpg,im01032011.jpg,im01042010.jpg,im01042012.jpg,im01052009.jpg,im01052011.jpg,im01062012.jpg,im01072008.jpg,im01072011.jpg";
$array = explode(",", $string); // rebulding your array ;

var_dump($array);

usort($array, function ($a, $b) {
    $a = DateTime::createFromFormat("mdY", substr(pathinfo($a, PATHINFO_FILENAME), 2));
    $b = DateTime::createFromFormat("mdY", substr(pathinfo($b, PATHINFO_FILENAME), 2));
    return ($a == $b) ? 0 : (($a < $b) ? - 1 : 1);
});

var_dump($array);

Output

Before

array
  0 => string 'im01012007.jpg' (length=14)
  1 => string 'im01022006.jpg' (length=14)
  2 => string 'im01022012.jpg' (length=14)
  3 => string 'im01032011.jpg' (length=14)
  4 => string 'im01042010.jpg' (length=14)
  5 => string 'im01042012.jpg' (length=14)
  6 => string 'im01052009.jpg' (length=14)
  7 => string 'im01052011.jpg' (length=14)
  8 => string 'im01062012.jpg' (length=14)
  9 => string 'im01072008.jpg' (length=14)
  10 => string 'im01072011.jpg' (length=14)

After

array
  0 => string 'im01022006.jpg' (length=14)
  1 => string 'im01012007.jpg' (length=14)
  2 => string 'im01072008.jpg' (length=14)
  3 => string 'im01052009.jpg' (length=14)
  4 => string 'im01042010.jpg' (length=14)
  5 => string 'im01032011.jpg' (length=14)
  6 => string 'im01052011.jpg' (length=14)
  7 => string 'im01072011.jpg' (length=14)
  8 => string 'im01022012.jpg' (length=14)
  9 => string 'im01042012.jpg' (length=14)
  10 => string 'im01062012.jpg' (length=14)

Upvotes: 1

complex857
complex857

Reputation: 20753

No built in function will do this out of the box, i would use usort like this:

usort($files, function($a, $b) {
    return strcmp(preg_replace('/^.*(\d{2})(\d{2})(\d{4})$/', '$3$1$2', $a), preg_replace('/^.*(\d{2})(\d{2})(\d{4})$/', '$3$1$2', $b));
});

Upvotes: 1

Mangala Edirisinghe
Mangala Edirisinghe

Reputation: 1111

Use substr() and divide your string into parte (day,month,year). Then You can sort them.

Upvotes: 1

SDC
SDC

Reputation: 14222

Yes there is. Convert it to a sensible format first.

I suggest using PHP's DateTime class and keeping all your dates as DateTime objects while you're in the PHP program.

If you don't want to do that, here's a function to sort them in as-is.

usort($myDates, function($a, $b) {
    $aBits = array_chunk($a,2);
    $bBits = array_chunk($a,2);
    $aDate = $aBits[3].$aBits[4].$aBits[2].$aBits[1];
    $bDate = $aBits[3].$aBits[4].$aBits[2].$aBits[1];
    return ($aDate < $bDate) ? -1 : 1;
});

Hope that helps.

Upvotes: 1

Related Questions