简体   繁体   中英

Turn abbreviated months in 4 different languages into corresponding month number

I have English, German, French and Spanish abbreviations of months and I must return it as a number

so 'dic' would be 12 //es

or 'avr' would be 4 //fr

I also have the locale that would be used

This is what the inputs looks like

<?php return array (
'de' => array (
    'Jan' => 'Jan',
    'Feb' => 'Feb',
    'März' => 'Mar',
    'Apr' => 'Apr',
    'Mai' => 'May',
    'Juni' => 'Jun',
    'Juli' => 'Jul',
    'Aug' => 'Aug',
    'Sep' => 'Sep',
    'Okt' => 'Oct',
    'Nov' => 'Nov',
    'Dez' => 'Dec',
),
'es' => array(
    'ene' => 'Jan',
    'feb' => 'Feb',
    'mar' => 'Mar',
    'abr' => 'Apr',
    'may' => 'May',
    'jun' => 'Jun',
    'jul' => 'Jul',
    'ago' => 'Aug',
    'sept' => 'Sep',
    'oct' => 'Oct',
    'nov' => 'Nov',
    'dic' => 'Dec',
),
'fr' => array(
    'janv' => 'Jan',
    'Feb' => 'Feb',
    'mars' => 'Mar',
    'avr' => 'Apr',
    'mai' => 'May',
    'juin' => 'Jun',
    'juil' => 'Jul',
    'aout' => 'Aug',
    'sept' => 'Sep',
    'oct' => 'Oct',
    'nov' => 'Nov',
    'dec' => 'Dec',
),
);

I have been messing around with

setLocale(LC_ALL,'de_DE.UTF-8)
//"de_DE.UTF-8"
echo  strftime("%A %d %B %Y", mktime(0, 0, 0, 12, 22, 1978));
//"Freitag 22 Dezember 1978"

I am using Carbon to parse the date ie

\Carbon::parse('Jan');

will give me

 Carbon\Carbon {#1866
 +"date": "2018-01-18 00:00:00.000000",
 +"timezone_type": 3,
 +"timezone": "UTC",}

I want to be able to do the same with the other languages.

You can do this:

$month = array_search('März', array_keys($months['de'])) + 1;
now()->month($month);

This will give you:

Illuminate\Support\Carbon @1521374393 {#871
    date: 2018-03-18 11:59:53.0 UTC (+00:00),
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM