Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

String to date sorting array

String to date sorting array

String to date sorting array

Hi, I'm a VB dev and this is my 1st day developing with PHP.

I've retrieved a list of files from a directory and stripped off the ".: and the file extensions
and also the 1st 3 letters of the file name itself since they are always constant (ex. "bbn10052005.htm").
What I'm left with is a string "10052015" to which I've added two slashes in it so it looks like a date :).
"10/05/2015". After loading it into an array and attempted to sort which didn't work.

I'm guessing I should convert into a date and then sort? Not sure about the correct way to convert or if i'm going in the right direction.

Ultimately I will stack the dates up in descending order. Right now I'm getting this


Any Ideas , thoughts or samples would be greatly appreciated.


RE: String to date sorting array

To sort these text strings without officially translating to a date format, you'd need to parse them as YYYYMMDD or a flavor of YYYY/MM/DD.

RE: String to date sorting array


If not needed to do anything else with them as with dates, no need to explicitly convert. usort() and a custom comparator will do it ( supposing the list is in variable $list ) :


usort($list, function($a, $b) { return strcmp(substr($a, 6, 4) . substr($a, 0, 5), substr($b, 6, 4) . substr($b, 0, 5)); }); 

Note that the callback will be executed for each needed comparison ( for the 11 item list you provided 30 times ). So do not use it for huge lists.


RE: String to date sorting array

Thank you both for the responses. Feherke you usort() dropped right in and worked as soon as I changed by arrays name to $list. There will only be a few dozen rows of data and at about 24 additions a year I can run with the 30 hits per.

Thanks again


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close