If the question is sorting the data list, I would use the function usort
.
It orders a array
according to a comparison made by callback
.
So what a beauty (a date has been modified to verify that sorting works):
$locados = array(
'2016-02-01', '2016-01-02', '2016-01-03', '2016-01-06',
'2016-01-07', '2016-01-08'
);
usort($locados, function ($a, $b)
{
return strtotime($a) - strtotime($b);
});
pr($locados);
The result will be:
Array
(
[0] => 2016-01-02
[1] => 2016-01-03
[2] => 2016-01-06
[3] => 2016-01-07
[4] => 2016-01-08
[5] => 2016-02-01
)
To get the last and the first date, we can do this:
function first($arr)
{
return reset($arr);
}
function last($arr)
{
return end($arr);
}
first($locados); // 2016-01-02
last($locados); // 2016-02-01
If you do not understand why creating new functions to get the last and the first element, I suggest you read this answer:
link