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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:03:08+00:00 2026-06-15T18:03:08+00:00

I’m sorting an associative arrays date properties (unix timestamps) using usort() . It’s working

  • 0

I’m sorting an associative arrays date properties (unix timestamps) using usort(). It’s working as intented. But I would like extend the functionality a bit (if possible). All the dates which are > “now” should be in the reverse order. In other words, I want the date closest to “now” to be on top of the list. Please let me know if I need to elaborate anything. My current code:

usort($DTO, function($a, $b) {
     return $b['date'] - $a['date'];
});
  • 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-15T18:03:09+00:00Added an answer on June 15, 2026 at 6:03 pm

    Thank goodness you’re using PHP 5.3 or better, doing this without first-class anonymous functions would have been a pain.

    Your sort should look something like this. I’ve replaced your variables with some sample data to demonstrate how it works.

    I could interpret your request in two ways. I’ll provide examples for both.

    $fake_dates = array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );
    $current_date = 5;
    $picky_date_sort = function($a, $b) use($current_date) { 
        if($a > $current_date && $b <= $current_date)
            return -1;
        if($b > $current_date && $a <= $current_date)
            return 1;
        if($a > $current_date || $b > $current_date)
            return $a - $b;
        return $b - $a; 
    };
    usort($fake_dates, $picky_date_sort);
    print_r($fake_dates);
    

    The test results in:

    Array
    (
        [0] => 6
        [1] => 7
        [2] => 8
        [3] => 9
        [4] => 5
        [5] => 4
        [6] => 3
        [7] => 2
        [8] => 1
        [9] => 0
    )
    

    Dates greater than “now” are at the top. They are sorted in ascending order, meaning as you go down the list, you go further into the future. When that section of the list is completed and you hit the past again, the descending sort returns, with newer dates higher than lower dates.

    However, it’s also possible to interpret your request as “I don’t care about splitting the past from the future, I just want closest to now at the top.” That’s easier. We’ll break out abs to get the absolute difference between the numbers.

    $absolute_diff_sort = function($a, $b) use($current_date) {
        $abs_b = abs($b - $current_date);
        $abs_a = abs($a - $current_date);
        return $abs_a - $abs_b;
    };
    usort($fake_dates, $absolute_diff_sort); print_r($fake_dates);
    

    results in

    Array
    (
        [0] => 5
        [1] => 6
        [2] => 4
        [3] => 7
        [4] => 3
        [5] => 8
        [6] => 2
        [7] => 1
        [8] => 9
        [9] => 0
    )
    

    Now (5) is at the top. As you go further down the list, the absolute difference between now and then increases, mixing both past and future dates.

    If neither of these interpretations is what you meant, you’ll need to clarify your question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

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.