5

The have the following lines (the months are in Russian):

"Ноябрь 20 2012"
"Декабрь 19 2012"
"Сентябрь 12 2012"
"Июнь 5 2012"
"Август 2 2012"

I want to convert them to the following format:

11.20.2012
12.19.2012
09.12.2012
06.05.2012
08.02.2012

The problem, of course, is that the month is in Russian, so what I've tried doesn't work.

$date = "Ноябрь 20 2012";
print_r(date_parse_from_format("F j Y", $date));

How can I parse dates with Russian months?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Artem
  • 183
  • 1
  • 3
  • 9
  • 1
    Did you try `str_replace` the russian names with pre-made English names? – Peon Nov 20 '12 at 14:36
  • I am not sure I fully understand the question, but you might want to look into [strftime](http://php.net/manual/en/function.strftime.php), [strtotime](http://www.php.net/manual/en/function.strtotime.php), [setlocale](http://php.net/manual/en/function.setlocale.php), and [str_replace](http://php.net/manual/en/function.str-replace.php). Together, I think they should be able to do the trick. – ACJ Nov 20 '12 at 14:40

3 Answers3

11

You can do a workaround like this:

$ru_month = array( 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь' );
$en_month = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );

print_r(date_parse_from_format("F j Y", str_replace($ru_month, $en_month, $date)));

PS: as others stated, there is a direct method, here is a duplicate of your question.

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
Peon
  • 7,902
  • 7
  • 59
  • 100
  • 1
    But it's October, not Oktober :)) – Georgy Liparteliani Aug 18 '14 at 19:40
  • 1
    Also why did you forget about april... :'( – Georgy Liparteliani Aug 18 '14 at 19:56
  • 1
    So, finally it would be: $ru_month = array( 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь' ); $en_month = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ); – BasTaller Sep 05 '14 at 09:21
3

first tell php to use your locale settings,

setlocale( LC_TIME, 'ru_RU', 'russian' );

then try to parse the string.

$date = "Ноябрь 20 2012";
print_r( date_parse_from_format("F j Y", $date) );
ncank
  • 946
  • 5
  • 15
  • local settings have already been configured to "russian", I just received the Russian line, but thanks for the reply :) – Artem Nov 20 '12 at 20:12
1

Use setlocale() and strftime() for non-english dates

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153