How To Sort An Array Of Dates In PHP?
Hello Friends,
This is example is how to sort an array of dates in php?
We are given an array which consists of multiple dates in (Y-m-d) format. We have to write a program in PHP to sort all the dates present in the array in decreasing order.
So let's start following example.
Example : 1
To sort an array of dates in PHP, the code is as follows.
<?php
function compareDate($date1, $date2){
return strtotime($date1) - strtotime($date2);
}
$dateArray = array("2021-11-11", "2021-10-10","2021-08-10", "2021-09-08");
usort($dateArray, "compareDate");
print_r($dateArray);
?>
Output:
This will produce the following output?
Array
(
[0] => 2021-08-10
[1] => 2021-09-08
[2] => 2021-10-10
[3] => 2021-11-11
)
It will help you....
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example