L
L
lukoie2014-08-21 12:15:12
PHP
lukoie, 2014-08-21 12:15:12

How to insert the name of two months on the page?

PHP page. It is necessary to write the name of the current and next month.
For example, in the phrase "... for August and September " you need the current and next month and in the case (!). How to do it correctly, in php or js?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WebSpider, 2014-08-21
@WebSpider

$months = array(
  1 => 'января',
  2 => 'февраля',
  ...
  12 => 'декабря'
);

echo $months[date('n')]; // текущий месяц
echo $months[date('n', strtotime('+1 month')]; // следующий месяц

E
Evgeny Petrov, 2014-08-21
@Petroveg

I do something like this in JS

function getMonth (index, modify) {
  var monthesRule = [
      ['(ь|й)', 'я'],
      ['т', 'та']
    ],
    result = [
      'Январь',
      'Февраль',
      'Март',
      'Апрель',
      'Май',
      'Июнь',
      'Июль',
      'Август',
      'Сентябрь',
      'Октябрь',
      'Ноябрь',
      'Декабрь'
    ][index - 1];

  if (modify) {
    for (var i = 0, r; i < monthesRule.length; i++) {
      r = new RegExp(monthesRule[i][0] + '$');

      if (r.test(result)) {
        return result.replace(r, monthesRule[i][1]);
      }
    }
  }

  return result;
}

console.log(getMonth(2));
console.log(getMonth(2, true));

A
Alexander Taratin, 2014-08-21
@Taraflex

habrahabr.ru/post/39327

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question