SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Sorting date array

    Array:
    45 , Play Tennis , 20/12/2006
    67 , Play Soccer , 21/12/2006
    90 , Have a Bath, 16/12/2006


    This array is unsorted... I need to sort it by date... and the date value is in spanish format dd/mm/YYYY.

    How can I have it sorted?

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function dosort($a,$b){
      
    $aa=strtotime($a['date']);
      
    $bb=strtotime($b['date']);
      if(
    $aa==$bb) return 0;
      else return 
    $aa<$bb?-1:1;
    }

    usort($array,'dosort'); 
    Saul

  3. #3
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In case strtotime doesn't work, use this:
    PHP Code:
    function dateCompare($a$b) {
        
    $aDate explode('/'$a[2]);
        
    $aDate mktime(000$aDate[1], $aDate[0], $aDate[2]);
        
    $bDate explode('/'$b[2]);
        
    $bDate mktime(000$bDate[1], $bDate[0], $bDate[2]);
        return(
    $aDate $bDate);

    }
    $array = array(array(45'Play Tennis''20/12/2006'),
                   array(
    7'Play Soccer''21/12/2006'),
                   array(
    90'Have a Bath''16/12/2006'));
    usort($array'dateCompare'); 

  4. #4
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry pal, it didn't work out... maybe Im missing something.

    print_r Before sorting...
    Array ( [0] => Array ( [0] => 43 [1] => 1 [2] => 12/01/2007 ) [1] => Array ( [0] => 64 [1] => 3 [2] => 15/05/2007 ) [2] => Array ( [0] => 74 [1] => 1 [2] => 01/05/2007 ) [3] => Array ( [0] => 71 [1] => 1 [2] => 15/12/2006 ) [4] => Array ( [0] => 71 [1] => 3 [2] => 23/05/2007 ) )

    print_r ...after sorting.
    Array ( [0] => Array ( [0] => 71 [1] => 3 [2] => 23/05/2007 ) [1] => Array ( [0] => 71 [1] => 1 [2] => 15/12/2006 ) [2] => Array ( [0] => 74 [1] => 1 [2] => 01/05/2007 ) [3] => Array ( [0] => 64 [1] => 3 [2] => 15/05/2007 ) [4] => Array ( [0] => 43 [1] => 1 [2] => 12/01/2007 ) )

    Here's he code that generates the array:
    PHP Code:
    while($rsmysql_fetch_array($rMYSQL_ASSOC)){
             
    $lista_registros[] = array($tablas[id],$rs[id],$rs[$fecha]);

    remember... date is in spanish format: dd/mm/YYYY

  5. #5
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whose code did you use?

    Use mine, it works.

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Did you change $a['date'] and $b['date'] to $a[2], $b[2]?

    Edit:

    No, use mine!!!
    Saul

  7. #7
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php_daemon,

    Did you test your code? strtotime() simply doesn't work with such a date format, as I expected.

  8. #8
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent work for both of you.... I tried Mike's and it worked, and after changing the code like php_deamon recommended (the bug was my fault) it worked too.

    Thanks!

  9. #9
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your date should be stored in mysql as a date field. If it were you'd be able to sort it in one query.

  10. #10
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    How Can I comparte if one date in spanish format (dd/mm/YYYY) is between two others spanish dates?

    if "13/12/2006" > "13/12/2007" and "13/12/2006" < "13/12/2005"

  11. #11
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Convert them to timestamps, either using strtotime or use Mike's proposed method.
    Saul

  12. #12
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    store your date in date fields instead of varchar or text and use msyql date_time functions.

  13. #13
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Those recomendations apply only to sql filled arrays. Im actually needing to know if one date is between two others..

    if "13/12/2006" > "13/12/2007" and "13/12/2006" < "13/12/2005"

    and the tree of them are form variables, so no SQL query can compare them

  14. #14
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Feudalism View Post
    Those recomendations apply only to sql filled arrays. Im actually needing to know if one date is between two others..

    if "13/12/2006" > "13/12/2007" and "13/12/2006" < "13/12/2005"

    and the tree of them are form variables, so no SQL query can compare them
    Sorry you mentioned mysql in a previous post so I assumed you were pulling them from a database.. in the case of user supplied dates through a form field use Mikes sorting function to sort an array of the three dates.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •