-2

I need to sort an array from the smallest date. I used Usort, but it order the array considering just the day. I tried using a code from an example using sort in javascript but i need this happen in php or find a way to convert php array to js.

Here is the code:

<?php
          ArrayDates ( [0] => 22/03/2018 [1] => 09/04/2018 [2] => 26/03/2018 
          [3] => 27/11/2017 [4] => 22/01/2018 [5] => 06/09/2017 )
?>
           <script>
           ArrayDates.sort(function (a, b){
                var aa = a.split('-'),
                    bb = b.split('-');

                return aa[2] - bb[2] || aa[1] - bb[1] || aa[0] - bb[0];
            })
          </script>
Hector
  • 119
  • 1
  • 1
  • 12
  • If you format your dates [properly](https://xkcd.com/1179/), then you can just `strcmp` them. It gets even better if you work with timestamps because those are just numbers you can sort directly. Only use `D/M/Y` format when outputting the result to the user. – Niet the Dark Absol Jun 23 '18 at 13:47
  • 2
    Looks like you used an example from Javascript. That's not valid PHP code. – rickdenhaan Jun 23 '18 at 13:47
  • yeah, this is probably javascript. the missing semicolons alone ... – Jakumi Jun 23 '18 at 13:51
  • That array definition is extremely odd. It seems like the `print_r` output rather than an actual array. – Script47 Jun 23 '18 at 13:55
  • Hi, yeap the array is come from print_r, i put it just as example. The dates are formated as spanish way and i need keep that way. I just realized that is a Javascript example, as rickdenhaad said. – Hector Jun 23 '18 at 14:00
  • Okay, if you want to sort the array in Javascript, you need to change your calls to `split()` to split on `/` instead of `-` (since there is no `-` in your date strings). – rickdenhaan Jun 23 '18 at 14:03
  • Niet the Dark Absol, Can u please give me an example? I used timestamps following this example: https://stackoverflow.com/questions/28688597/how-can-i-find-the-maximum-and-minimum-date-in-an-array but it works just if it is the same year. – Hector Jun 23 '18 at 14:05

2 Answers2

0

Hi finally i found a solution in a forum and maybe can help to someone else. The solution is create a new array of timestamps based on the original array. Then sort this new array. After just echo the first element of the new array with "date" and will return the first date. Here is the code:

  <?php
            $ArrayDates= array ('22/03/2018','09/04/2018', '26/03/2018', 
            '27/11/2017','22/01/2018', '06/09/2017');

            function date_to_timestamp($d){
                $newarr = array();
                foreach($d as $f) {
                $arr=explode("/",$f);
                array_push($newarr, mktime(0,0,0,$arr[0],$arr[1],$arr[2]));
                }

                return $newarr;
            } 

            function cmp2($a, $b)
                {
                if ($a == $b) {
                return 0;
                }
                return ($a < $b) ? -1 : 1;
            }

            $third = date_to_timestamp($ArrayDates);

            usort($third, "cmp2");

            echo date('m/d/Y', $third[0]);
  ?>
Hector
  • 119
  • 1
  • 1
  • 12
0

This will sort the array of date in ascending order.

$date = array('23-02-2012','21-01-2014','11-01-2010','09-02-2001','01-01-2019');  

function date_sort($a, $b) {
    return strtotime($a) - strtotime($b);
}

usort($date, "date_sort");

print_r($date);

Output:-

Array ( 
         [0] => 09-02-2001 
         [1] => 11-01-2010 
         [2] => 23-02-2012 
         [3] => 21-01-2014 
         [4] => 01-01-2019 
      )
Shanu Singh
  • 101
  • 2
  • 2