Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8452493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:30:50+00:00 2026-06-10T11:30:50+00:00

I am trying to sort the arrays (each containing 2 date values) in a

  • 0

I am trying to sort the arrays (each containing 2 date values) in a multidimensional array. I was able to find a useful function which solves the question for one element, however I was unable to modify it for two.

PHP Sort a multidimensional array by element containing date

function date_compare($a, $b)
{
    $t1 = strtotime($a['datetime']);
    $t2 = strtotime($b['datetime']);
    return $t1 - $t2;
}    
usort($array, 'date_compare');

The problem at hand is sorting comments in which have a post time and an edit time. Essentially I want to sort them from newest to oldest (while keeping both values).

If this is not possible, let me know.

EDIT: Mockup

$array = array(
    [0] => array(
          [0] => "Aug:1:2012 12:00:pm", // post date
          [1] => "Aug:28:2012 12:00:pm"  // edit date
    ),
    [1] => array(
          [0] => "Aug:1:2012 12:00:pm",
          [1] => "Aug:30:2012 12:00:pm"
    )
    [2] => array(
          [0] => "Aug:29:2012 12:00:pm",
          [1] => "Aug:1:2012 12:00:pm"
    )
};

Should output: $array[1] first (because it has the highest date out of keys 1 & 2) then $array[2], then $array[0].

$array = array(
    [0] => array(
          [0] => "Aug:1:2012 12:00:pm",
          [1] => "Aug:30:2012 12:00:pm" // highest   
    ),
    [1] => array(
          [0] => "Aug:29:2012 12:00:pm", // next
          [1] => "Aug:1:2012 12:00:pm"
    )
    [2] => array(
          [0] => "Aug:1:2012 12:00:pm",
          [1] => "Aug:28:2012 12:00:pm" // lowest
    )
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:30:52+00:00Added an answer on June 10, 2026 at 11:30 am

    I’m not sure if understood correctly but you mean you want sort your array base on column1 (“post date”) and if these values are equal order is determined by column2. So, what you need is just fix your comparing function:

    function date_compare($a, $b)
    {
        $t1 = strtotime($a['datetime']);
        $t2 = strtotime($b['datetime']);
        if ($t1 != $t2) {
            return $t1 - $t2;
        }
    
        // if "post dates" are equal, compare "edit dates"
        $t1 = strtotime($a['datetime2']);
        $t2 = strtotime($b['datetime2']);
    
        return $t1 - $t2;
    }    
    

    edit:

    ok, according to your comment, you just need to pick up max element from your array. So, this should work:

    usort($array, function($a, $b) {
        $t1 = max(strtotime($a[0]), strtotime($a[1]));
        $t2 = max(strtotime($b[0]), strtotime($b[1]));
    
        return $t1 - $t2;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort an array in PHP by date and time which
I'm trying to sort a multidimensional array in objective-c i know that i can
I need to create a sort of 2D array in which each one of
I am trying to sort an array of structures by each member of the
Trying to sort by this multidimensional array by screen name: //Looping through this for
I am trying to sort array with usort, but it doesn't work, What am
Trying to sort this array of objects according to (1) depth and (2) weight,
I'm trying to sort any array with array_multisort() and everything is working great. However,
I am trying to sort an array. I have an array of data, where
I'm trying to sort an array, filled with objects from a class i wrote.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.