Sort vector from lowest to highest

0
  PHP:
    array_push($var, $linha['data']);
    array_push($var, $linha['data_t']);
    array_push($var, $linha['data_f']);

    usort($var);// Adicionei essa linha para tentar formatar as datas da menor pra maior mas retornou o erro 

Warning: usort () expects exactly 2 parameters, 1 given

    $var = array_unique($var); 

    foreach ($var as $v) {
    echo  "<td><div class='data'>" . $v . "</div></td>";
    }





  JS:
  var data = [];
  for(var i = 0; i < $(".data").length ; i++)
  { 
     data[i] =  $(".data").eq(i).text();
  }

I have this loop that takes the php date.
The problem is that at the time of printing it prints for example: 02 / 05,05 / 05,01 / 05. I would like to sort the variable data[] in ascending order of date. I get the date in a Sql in date format (% dd /% mm) ie 00/00

    
asked by anonymous 29.08.2018 / 22:41

1 answer

1
//função para ordenar
function comparaPorTimeStamp($time1, $time2)
{
    if (strtotime($time1) > strtotime($time2))
        return 1;
    else if (strtotime($time1) < strtotime($time2)) 
        return -1;
    else
        return 0;
}


$var = array("12/08", "13/08", "06/08");

array_push($var, "10/08");
array_push($var, "15/08");
array_push($var,"11/08");       

$var = array_unique($var);  

$lista = array();

foreach( $var as $value ) {
    //inverte dia com mes e substitui por traço
    $value= join("-",array_reverse(explode("/",$value)));

    /*******já que só tem dia e mês podemos acrescentar qualquer ano para
    formar uma data digna de ser comparada *****************************/

    $arquivo= "2018-".$value;
    //cria o array
    $lista[] .= $arquivo;
}       

//ordena por data crescente
usort($lista, "comparaPorTimeStamp");

/*******************************
vamos ao que interessa ↓
*******************************/

$datas = array();

foreach( $lista as $valor ) {
    $valor=str_replace("2018-","",$valor);
    $valor= join("/",array_reverse(explode("-",$valor)));
    $datas[] .= $valor;
}

print_r($datas);

example running on ideone

strtotime - accepts a string, in the format "date / time in English ", and performs a parse on it by turning it into a timestamp.

What is Timestamp (unix time)?

Timestamp (or Unix Time) is the integer that represents the number of seconds that have passed since January 1, 1970 based on the Greenwich Mean Time (GMT), which is zero (zero) time zone.

usort - Sorts an array by values using a comparison function defined by the user

    
30.08.2018 / 03:52