I like @Rizier123's solution so I thought I'd write up an implementation.
First, let's convert the months array into numerical values which represent the month. We're going to preserve the text as the key to make the matching process easier at the end. If you have control over those months then it's quite simple:
$months = [ 'mar' => 3, 'jun' => 6, 'sep' => 9, 'dec' => 12];
If you don't have control over the array you'll need to run it through array_map()
and use date to convert:
$month_keys = $months;
$months = array_map( function( $month ) {
return date( 'm', strtotime( $month ) );
}, $months );
$months = array_combine( $month_keys, $months );
Then let's find the next closest value in the array:
$closest_month = null;
foreach ( $months as $month_text => $month_num ) {
if ( $current_month <= $month_num ) {
$closest_month = $month_text;
break;
}
}
$closest_month
should now match all of the conditions set out in your question.