Jump to content

Recommended Posts

Ok, I have several directory names all are in the format dd-mm-yyyy-name

I substr()'d the date off, and removed the hyphens. So the format was ddmmyyyy
I placed the substring and full dir name in an array, the substring integers were the keys for their corresponding directories.

I then did a ksort($array)

And it comes back sorted, but it only takes account of the first two numbers of the integer.

Meaning I get them back in the right order if only going by the days. i.e

01-01-05
02-01-04
13-10-06

I looked into using strtotime() but it seemed to not be entirely suited here, considering I have an almost complete date, and it seems to want input more in the way of "next tuesday" than an actual date.

Thank you.

Gaz
OK, you were on the right track with strtotime();

Here's some sample code I just knocked up, you could even put it into a function if you wanted.

[code]<?php

// Test data (unordered)
$folders = array('01-01-1970-first', '05-01-1970-second', '10-06-2000-fifth', '05-03-1985-third', '19-12-1993-fourth');

// Loop through each folder
foreach ($folders as $name){
  preg_match('/^(\d{2})-(\d{2})-(\d{4})/', $name, $segments); // match each segment of the date
  $gnu_date_format = $segments[3].$segments[2].$segments[1]; // put date in gnu format (yyyymmdd) for passing to strtotime()
  $timestamp = strtotime($gnu_date_format); // change the date format to a unix timestamp
  $newlist[$timestamp] = $name; // new array keyed on timestamp
}

// Sort the new list
ksort($newlist);

// Echo the folder names
foreach ($newlist as $folder){
  echo "$folder<br>\n";
}
?>[/code]

Regards
Huggie
Thanks alot! That did the job perfectly.

If you don't mind baring with me a second I don't entirely understand the script.

preg_match('/^(\d{2})-(\d{2})-(\d{4})/', $name, $segments);

I assume returns 3 thins, I assume the 3 sections of date delimited by the hypens... and puts them into the array $segments[]
$gnu_date_format just moves them around. The rest I understand.

Just a little point in the right direction of how that regex works would be much appreciated.

(It works perfectly too btw).

Thank you so much!

Gaz
ok, here it is broken down.  I've only included the first half as once you have that the rest is self explanatory

[table]
[tr][td][color=red]/[/color][/td][td]Start the pattern sequence[/td][/tr]
[tr][td][color=red]^[/color][/td][td]Match the start of the line[/td][/tr]
[tr][td][color=red](\d{2})[/color][/td][td]Match 2 digits, enclosing inside the parenthesis says we want to capture that specific data into a variable[/td][/tr]
[tr][td][color=red]-[/color][/td][td]Match a hyphen[/td][/tr]
[tr][td][color=red](\d{2})[/color][/td][td]Match another 2 digits, again capturing them into a separate variable[/td][/tr]
[tr][td][color=red]-[/color][/td][td]Match a hyphen[/td][/tr]
[tr][td][color=red](\d{4})[/color][/td][td]Match the last 4 digits, again, put them into a separate variable[/td][/tr]
[tr][td][color=red]/[/color][/td][td]End the pattern matching sequence[/td][/tr]
[/table]

Regards
Huggie
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.