php - Sorting dates with months or days on 0 -
i have sort dates on php, in of them can have '0' on month or day, example:
1951-11-00
1951-12-14
problem dates don't sorted other dates have year, day , month values.
first tried using array_multisort:
foreach ($objects $key => $part) { $sort[$key] = strtotime($part['object']['date']); } array_multisort($sort, sort_desc, $objects );
it sort ok, first sorts values have 00 on month or day, , sort normal dates, example:
1937-05-00
1937-04-00
1935-11-00
1951-12-16
1951-12-14
as see, order not ok, because dates 1951 should go first, sort algorithm classifying dates in 2 groups, ones have 00, , ones have full numbers.
then tried uasort, i'm not getting results.
how can sort dates correctly? list put, need show on order (regardless of having month on 00):
1951-12-16
1951-12-14
1937-05-00
1937-04-00
1935-11-00
just sort them strings. in comparable format when compared strings.
$dates = [ '1937-05-00', '1937-04-00', '1935-11-00', '1951-12-16', '1951-12-14' ]; usort($dates, function($a, $b) { return $a < $b; }); var_export($dates);
Comments
Post a Comment