Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,406 questions

56,397 answers

573 users

How to sort an array of dates in descending order with PHP

1 Answer

0 votes
function compareByTimeStamp($time1, $time2) { 
    if (strtotime($time1) < strtotime($time2)) 
        return -1; 
    else if (strtotime($time1) > strtotime($time2))  
        return 1; 
    else
        return 0; 
} 
  
$arr = array("13-02-2007",
             "19-04-2011",               
             "18-04-2011",
             "01-03-1966",
             "17-05-2019",
             "17-12-1980",
             "21-06-2017",
             "17-05-2020"); 
             
usort($arr, "compareByTimeStamp"); 
  
print_r($arr); 



/*
run:

Array
(
    [0] => 01-03-1966
    [1] => 17-12-1980
    [2] => 13-02-2007
    [3] => 18-04-2011
    [4] => 19-04-2011
    [5] => 21-06-2017
    [6] => 17-05-2019
    [7] => 17-05-2020
)

*/

 



answered May 19, 2020 by avibootz
edited May 19, 2020 by avibootz

Related questions

2 answers 377 views
1 answer 298 views
1 answer 250 views
2 answers 247 views
1 answer 231 views
...