1

Код:

if ($moth == 'Jan' or $moth_end == 'Jan') {
    $moth = 'Январь'; $moth_end = 'Январь';
}
elseif ($moth == 'Feb' or $moth_end == 'Feb') {
    $moth = 'Февраль'; $moth_end = 'Февраль';
}
elseif ($moth == 'Mar' or $moth_end == 'Mar') {
    $moth = 'Март'; $moth_end = 'Март';
}
elseif ($moth == 'Apr' or $moth_end == 'Apr') {
    $moth = 'Апрель'; $moth_end = 'Апрель';
}
elseif ($moth == 'May' or $moth_end == 'May') {
    $moth = 'Май'; $moth_end = 'Май';
}
elseif ($moth == 'Jun' or $moth_end == 'Jun') {
    $moth = 'Июнь'; $moth_end = 'Июнь';
}
elseif ($moth == 'Jul' or $moth_end == 'Jul') {
    $moth = 'Июль'; $moth_end = 'Июль';
}
elseif ($moth == 'Aug' or $moth_end == 'Aug') {
    $moth = 'Август'; $moth_end = 'Август';
}
elseif ($moth == 'Sep' or $moth_end == 'Sep') {
    $moth = 'Сентябрь'; $moth_end = 'Сентябрь';
}
elseif ($moth == 'Oct' or $moth_end == 'Oct') {
    $moth = 'Октябрь'; $moth_end = 'Октябрь';
}
elseif ($moth == 'Nov' or $moth_end == 'Nov') {
    $moth = 'Ноябрь'; $moth_end = 'Ноябрь';
}
elseif ($moth == 'Dec' or $moth_end == 'Dec') {
    $moth = 'Декабрь'; $moth_end = 'Декабрь';
}

Можно ли как то уменьшить количество символов? Или есть какая-нибудь альтернатива?

3
  • 1
    Использовать массив, очевидно.
    – becouse
    6 янв 2020 в 6:38
  • @becouse и через foreach искать) 6 янв 2020 в 6:59
  • 1
    Ассоциативный массив. Ключ dec значение Декабрь.
    – becouse
    6 янв 2020 в 7:07

1 ответ 1

4
$moth = null;
$moth_end ='Feb';

function getMonthName($moth, $moth_end) {
    $monthsMap = [
        'jan' => 'Январь',
        'feb' => 'Февраль',
        'mar' => 'Март',
    ];

    $monthKey = $moth ?? $moth_end;

    return $monthsMap[strtolower($monthKey)];    
}

echo getMonthName($moth, $moth_end); // Февраль

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.