Jump to content

Sort by Date


doc1355
Go to solution Solved by Barand,

Recommended Posts

I have the following array, I want to sort by Date, from latest to oldest. How can I do that

	$array = array(
		0 => array(
			'id' => 5952,
			'points' => 15,
			'result' => 'W',
			'date' => 'Jan. 2, 22',
		),
		1 => array(
			'id' => 5965,
			'points' => 6,
			'result' => 'L',
			'date' => 'Jan. 1, 22',
		),
	);

 

Link to comment
Share on other sites

that date format is not directly sortable. where is this data coming from/stored at?

you should store dates in a YYYY-MM-DD format, which will allow easy sorting by the date, then output the date in the format that you want when you display it.

if this data is coming from a database, sort it in the sql query.

if this data must be sorted in php, you would use php's usort() function, with a call-back function to sort on the date field. if that  incoming date format cannot be corrected at the source, i would first convert the format, all at once, to be YYYY-MM-DD, then sort the data, then format the date back to that format when you display it.

  • Thanks 1
Link to comment
Share on other sites

  • Solution

Example

$arr = [
          [ 'A', 'Jan. 22, 22'],
          [ 'B', 'Dec. 25, 21'],
          [ 'C', 'Feb. 22, 22'],
          [ 'D', 'Jan. 2, 22']
       ];
usort($arr, function($a, $b) {
               $da = DateTime::createFromFormat('M. j, y', $a[1]);
               $db = DateTime::createFromFormat('M. j, y', $b[1]);
               return $db <=> $da;
            });
echo '<pre>' . print_r($arr, 1) . '</pre>';

outputs

Array
(
    [0] => Array
        (
            [0] => C
            [1] => Feb. 22 22
        )

    [1] => Array
        (
            [0] => A
            [1] => Jan. 22 22
        )

    [2] => Array
        (
            [0] => D
            [1] => Jan. 2 22
        )

    [3] => Array
        (
            [0] => B
            [1] => Dec. 25 21
        )

)

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

6 hours ago, mac_gyver said:

that date format is not directly sortable. where is this data coming from/stored at?

you should store dates in a YYYY-MM-DD format, which will allow easy sorting by the date, then output the date in the format that you want when you display it.

if this data is coming from a database, sort it in the sql query.

if this data must be sorted in php, you would use php's usort() function, with a call-back function to sort on the date field. if that  incoming date format cannot be corrected at the source, i would first convert the format, all at once, to be YYYY-MM-DD, then sort the data, then format the date back to that format when you display it.

Thank you. 

I got it to work. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.