Results 1 to 13 of 13

Thread: [RESOLVED] Sort array by date - date is in unusual format

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Resolved [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">&nbsp;';
    foreach(
    glob($Mydir.'*'GLOB_ONLYDIR) as $dir) {
        
    $dir str_replace($Mydir''$dir);
        echo 
    '<a href="sigma_rxpics/'.$dir.'">'.$dir.'</a>&nbsp; '// 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.

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Sort array by date - date is in unusual format

    You can try using strtotime()

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: Sort array by date - date is in unusual format

    Quote Originally Posted by dclamp View Post
    You can try using strtotime()
    Thanks for the suggestion. I'll look into it.

  4. #4
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    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?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    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.

  6. #6
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    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.

  7. #7
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    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">&nbsp;'

    $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>&nbsp; '// Create links to all folders in directory
    }
    echo 
    '</div><br>'

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: Sort array by date - date is in unusual format

    Quote Originally Posted by dclamp View Post
    Try this out. I haven't tested it but it should work.

    PHP Code:
    $Mydir 'sigma_rxpics/'
    echo 
    '<div class="archive-links">&nbsp;'

    $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>&nbsp; '// Create links to all folders in directory
    }
    echo 
    '</div><br>'
    Many thanks. I'll try it out tonight.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    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>&nbsp; '; // Create links to all folders in directory
    Last edited by paulg4ije; Jan 19th, 2015 at 01:44 PM.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    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".

  11. #11
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    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.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    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.


  13. #13
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    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
  •  



Click Here to Expand Forum to Full Width