-
Jan 17th, 2015, 11:59 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Sort array by date - date is in unusual format
I have a few dozen directories with names like jan2015, dec2014, nov2014 and so on. The format is always exactly like that; 3 lower case letters for the month and four digits for the year. I create a list of links to these directories like this:
PHP Code:
$Mydir = 'sigma_rxpics/';
echo '<div class="archive-links"> ';
foreach(glob($Mydir.'*', GLOB_ONLYDIR) as $dir) {
$dir = str_replace($Mydir, '', $dir);
echo '<a href="sigma_rxpics/'.$dir.'">'.$dir.'</a> '; // Create links to all folders in directory
}
echo '</div><br>';
This works great but the links are listed in alphabetical order. I would like the list in date order with the most recent date (month) first in the list. I could do this in VB but my PHP is weak. Can anyone help?
Thanks.
-
Jan 18th, 2015, 06:50 AM
#2
Re: Sort array by date - date is in unusual format
You can try using strtotime()
-
Jan 18th, 2015, 10:51 AM
#3
Thread Starter
Fanatic Member
Re: Sort array by date - date is in unusual format
Originally Posted by dclamp
Thanks for the suggestion. I'll look into it.
-
Jan 18th, 2015, 05:15 PM
#4
Re: Sort array by date - date is in unusual format
That function simply will turn the date string into a unix timestamp. Do you also need help sorting by date?
-
Jan 19th, 2015, 03:07 AM
#5
Thread Starter
Fanatic Member
Re: Sort array by date - date is in unusual format
Yes please! I guess the first thing I need to do is to create the array before the for-each loop, then sort it. I only had a brief look at strtotime() and I'm not sure it will directly handle my date format. Perhaps it might need a bit of string manipulation first, which shouldn't be too hard as my format is fixed.
Thanks for any additional help you can offer.
-
Jan 19th, 2015, 03:27 AM
#6
Re: Sort array by date - date is in unusual format
I tested out strtotime() on the format you provided. "dec2014" outputs the correct date timestamp as 12-1-2014.
Im heading away from my computer now, but I will get back to your afterward.
-
Jan 19th, 2015, 06:26 AM
#7
Re: Sort array by date - date is in unusual format
Try this out. I haven't tested it but it should work.
PHP Code:
$Mydir = 'sigma_rxpics/'; echo '<div class="archive-links"> ';
$mydirectories = array();
foreach(glob($Mydir.'*', GLOB_ONLYDIR) as $dir) { $dir = str_replace($Mydir, '', $dir); $date = strtotime($dir); $mydirectories[$dir] = $date; }
asort($mydirectories);
foreach($mydirectories as $key->$value) { $date = date("m-Y", $value); echo '<a href="sigma_rxpics/'.$key.'">'.$key.'</a> '; // Create links to all folders in directory } echo '</div><br>';
-
Jan 19th, 2015, 09:40 AM
#8
Thread Starter
Fanatic Member
Re: Sort array by date - date is in unusual format
Originally Posted by dclamp
Try this out. I haven't tested it but it should work.
PHP Code:
$Mydir = 'sigma_rxpics/';
echo '<div class="archive-links"> ';
$mydirectories = array();
foreach(glob($Mydir.'*', GLOB_ONLYDIR) as $dir) {
$dir = str_replace($Mydir, '', $dir);
$date = strtotime($dir);
$mydirectories[$dir] = $date;
}
asort($mydirectories);
foreach($mydirectories as $key->$value) {
$date = date("m-Y", $value);
echo '<a href="sigma_rxpics/'.$key.'">'.$key.'</a> '; // Create links to all folders in directory
}
echo '</div><br>';
Many thanks. I'll try it out tonight.
-
Jan 19th, 2015, 01:36 PM
#9
Thread Starter
Fanatic Member
Re: Sort array by date - date is in unusual format
First try gives this error:
Fatal error: Cannot access empty property in ....... .php on line 55
Line 55 (according to Notepad++) is: foreach($mydirectories as $key->$value) {
I'll do some research but you might beat me to it :-)
EDIT:
Might have fixed that one by changing to $key->value but now I get:
"Catchable fatal error: Object of class stdClass could not be converted to string in ...... .php on line 57", which is:
echo '<a href="sigma_rxpics/'.$key.'">'.$key.'</a> '; // Create links to all folders in directory
Last edited by paulg4ije; Jan 19th, 2015 at 01:44 PM.
-
Jan 19th, 2015, 01:57 PM
#10
Thread Starter
Fanatic Member
Re: Sort array by date - date is in unusual format
I wondered if you meant to put $date in line 57 rather than $key, but then I just get lots of links to "01-1970".
-
Jan 19th, 2015, 04:49 PM
#11
Re: Sort array by date - date is in unusual format
My bad. that should be $key=>$value Notice the equal sign.
I was thinking OOP.
-
Jan 19th, 2015, 05:07 PM
#12
Thread Starter
Fanatic Member
Re: Sort array by date - date is in unusual format
That's looking very good - except the order is reversed to what I want. I'd like the most recent date first. I'll start googling ...
EDIT: Got it! arsort rather than asort. Many thanks for your help.
-
Jan 19th, 2015, 09:32 PM
#13
Re: Sort array by date - date is in unusual format
No problem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|