PHP re-order array of month names

Convert the months to a numerical value. Then order your array numerically using sort()

You can use this question: convert month from name to number

@Matthew’s answer seems to work well:

$date = date_parse('July');;
echo $date["month"];

Working solution

$months = array("April", "August", "February");

usort($months, "compare_months");
var_dump($months);

function compare_months($a, $b) {
    $monthA = date_parse($a);
    $monthB = date_parse($b);

    return $monthA["month"] - $monthB["month"];
}

Leave a Comment