Dude, the date format is irrelevant, I already mentioned I want to use DateTime objects and provided a link with the code to sort using those.
If you're using DateTime objects, the format is irrelevant.
Barand solution is not the solution I'll be using, I was only pointing out the issue was HOW to get the percentage when the condition is met more than once.
I was not asking how I can sort the array, but if I need to, is there another way.
Barand is doing string comparison, which of course is totally wrong when dealing with dates, I would advise to always stick to DateTime objects when dealing with datetime in PHP.
Again, I already indicated a solution of sorting using DateTime objects:
usort($array, function($a, $b) {
return $a['date']->format('U') - $b['date']->format('U');
});
Or, another example:
usort($arr, function($a, $b) {
$ad = new DateTime($a['dateTime']);
$bd = new DateTime($b['dateTime']);
if ($ad == $bd) {
return 0;
}
return $ad < $bd ? -1 : 1;
});
You could also use this if you're worried about format:
foreach ($data as $rec) {
if ($today <= DateTime::createFromFormat('d-m-Y', $rec['beforedate'])) {
$discount = $rec['percentage'];
break;
}
}
Take it easy, Just because Barand didn't provide a solution using DateTime objects it doesn't mean it would never work. I certainly do not want strcomp or strtotime