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 6048845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:30:33+00:00 2026-05-23T07:30:33+00:00

Possible Duplicate: PHP Sort a multidimensional array by element containing date I have some

  • 0

Possible Duplicate:
PHP Sort a multidimensional array by element containing date

I have some data from XML or JSON in a PHP array that looks like this:

[0]= array(2) {
    ["title"]= string(38) "Another title"
    ["date"]= string(31) "Fri, 17 Jun 2011 08:55:57 +0200"
}
[1]= array(2) {
    ["title"]= string(38) "My title"
    ["date"]= string(31) "Mon, 16 Jun 2010 06:55:57 +0200"
}

What I want to do is order the two items by date.

  1. Is it possible to sort by date, when the sort value is inside every item?
  2. Do I need to convert the date format to timestamp?

What I don’t want to do

I could use date and set it as the ID but that don’t feel right, because two items can have the same date and then it would not be unique.

  • 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-05-23T07:30:34+00:00Added an answer on May 23, 2026 at 7:30 am

    You don’t need to convert your dates to timestamps before the sorting, but it’s a good idea though because it will take more time to sort without this step.

    $data = array(
        array(
            "title" => "Another title",
            "date"  => "Fri, 17 Jun 2011 08:55:57 +0200"
        ),
        array(
            "title" => "My title",
            "date"  => "Mon, 16 Jun 2010 06:55:57 +0200"
        )
    );
    
    function sortFunction( $a, $b ) {
        return strtotime($a["date"]) - strtotime($b["date"]);
    }
    usort($data, "sortFunction");
    var_dump($data);
    

    Update

    In newer PHP versions you can use arrow functions too. Here you can find a more concise version of the above:

    usort($data, fn ($a, $b) => strtotime($a["date"]) - strtotime($b["date"]));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: php date compare I have a date that I take from the
Possible Duplicate: Sorting an associative array in PHP Hello all, I have an array
Possible Duplicate: PHP str_replace with for loop from array How to replace a string
Possible Duplicate: PHP - How to split a paragraph into sentences. I have a
Possible Duplicate: PHP: Strip a specific tag from HTML string? Hi All, I want
Possible Duplicate: Why PHP variables start with a $ sign symbol? I have looked
Possible Duplicates: reordering array attributes then save back to XML in php php loop
Possible Duplicate: PHP make if shorter I have an if statement that looks like
Possible Duplicate: PHP validation/regex for URL Hi, in my form I have a url
Possible Duplicate: php warning mysql_fetch_assoc I have a weird problem about my script. It

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.