Gazdaman Posted January 25, 2007 Share Posted January 25, 2007 Ok, I have several directory names all are in the format dd-mm-yyyy-nameI substr()'d the date off, and removed the hyphens. So the format was ddmmyyyyI 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.e01-01-0502-01-0413-10-06I 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 Quote Link to comment https://forums.phpfreaks.com/topic/35619-strange-chronological-order-array-sorting-problem/ Share on other sites More sharing options...
HuggieBear Posted January 25, 2007 Share Posted January 25, 2007 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 folderforeach ($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 listksort($newlist); // Echo the folder namesforeach ($newlist as $folder){ echo "$folder<br>\n";}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/35619-strange-chronological-order-array-sorting-problem/#findComment-168802 Share on other sites More sharing options...
Gazdaman Posted January 25, 2007 Author Share Posted January 25, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/35619-strange-chronological-order-array-sorting-problem/#findComment-168889 Share on other sites More sharing options...
HuggieBear Posted January 25, 2007 Share Posted January 25, 2007 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]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/35619-strange-chronological-order-array-sorting-problem/#findComment-168935 Share on other sites More sharing options...
Gazdaman Posted January 25, 2007 Author Share Posted January 25, 2007 What a guy, thank you.Gaz Quote Link to comment https://forums.phpfreaks.com/topic/35619-strange-chronological-order-array-sorting-problem/#findComment-169022 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.