-1

I have an array of date like

    a[0]=>2013-10-05
    a[1]=>2013-10-25
    a[2]=>2013-10-15
    a[3]=>2013-10-28

I want to sort in ascending order. How can i sort this?

2
  • 2
    What you tried so far? Commented Oct 29, 2013 at 6:16
  • PHP has many functions for sorting arrays, see php.net/manual/en/array.sorting.php Have you tried them? For example what did asort() return and why that did not match your expectations? Commented Oct 29, 2013 at 6:18

2 Answers 2

2

Try this;

$orderByDate = $my2 = array();
foreach($data as $key=>$row)
{
    $my2 = explode('-',$row[1]);
    $my_date2 = $my2[1].'-'.$my2[0].'-'.$my2[2];        
    $orderByDate[$key] = strtotime($my_date2);  
}    
array_multisort($orderByDate, SORT_ASC, $data);
Sign up to request clarification or add additional context in comments.

Comments

1

Try the default sort function as stated here: Sort by Date

Or use a custom sort function like suggested here: Custom Search Function

function sortFunction( $a, $b ) 
{
    //$a and $b are two values from your array
    //Return a value > 0 then $a is greater than $b
    return strtotime($a) - strtotime($b);
}
usort($data, "sortFunction");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.