Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Months In Russian

Tags:

date

php

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?

like image 767
Artem Avatar asked Nov 20 '12 14:11

Artem


3 Answers

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.

like image 200
Peon Avatar answered Oct 02 '22 15:10

Peon


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) );
like image 41
ncank Avatar answered Oct 02 '22 15:10

ncank


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

like image 32
Shakti Singh Avatar answered Oct 02 '22 17:10

Shakti Singh