Change language

Sort array of dates in PHP

Examples:
Input: array ("2018-06-04", "2014-06-08", "2018-06-05") Output: 2018-06-05 2018-06 -04 2014-06-08 Input: array ("2016-09-12", "2009-09-08", "2009-09-09") Output: 2016-09-12 2009-09-09 2009-09 -08 
To solve this problem in C / C++ / Java or any other general purpose programming language, we must either compare dates based on year, then month, and finally according to days. or by storing them in any structure or any other required data structure. But in PHP this problem seems very simple if we use the strtotime() function. The strtotime() function - this is a PHP function that changes a given date in any format to a timestamp that is a large integer and then sorting the array we can easily use PHP | Usort() function by defining a comparator function. The comparator function will take two date arguments, which will be converted to an integer timestamp using the strtotime() function, and then compared to the sort date based on the integer timestamp value. Built-in functions used:
  • This function changes the given date string to a timestamp (large int).
  • This function sorts the given array according to a custom comparison function.
Below is the PHP implementation of the above idea:
// PHP program for sorting an array of dates 
// custom compare function
// based on timestamp function compareByTimeStamp ( $time1 , $time2 ) { if ( strtotime ( $time1 ) < strtotime ( $time2 )) return 1 ; else if ( strtotime ( $time1 ) > strtotime ( $time2 )) return - 1; else return 0; }  
// Input Array $arr = array ( "2016-09-12" , "2009-09-06" , "2009-09 -09 " );  
// sort the array with a user-defined function usort ( $arr , "compareByTimeStamp" ); print_r ( $arr );  
?>
Output:
Array ([ 0] = > 2016-09-12 [1] = > 2009-09-09 [2] = > 2009-09-06) 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically