Sort vector from smallest to largest

Asked

Viewed 509 times

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 an Sql in date format(%dd/%mm) ie 00/00

  • The date is just that? I’ll wait until you ask the question correctly. The previous one you asked me raised doubts in the answer. This is not a valid date, it’s a string

  • The variable receives a date, I would just like to order increasingly

  • yy is year is right?

  • date is something like this 2018-08-12

  • Forgiveness is day and month

  • It is already in the correct format example 30/05 with the "/" tbm

  • Right, obgd for the attention

  • I used the usort function in php, it ordered but a date 00/00 was in front of the other kk

  • and where this date 00/00 ?

  • It must be some data that is without date, I have to check further sinking. There was a coincidence of the search that I made come with the dates in sequence

Show 5 more comments

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 "date/time" format and parses it into a timestamp.

What is Timestamp (Unix time)?

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.