如何将英德法西四语言月份缩写转为数字?Carbon多语种解析
Great question! Parsing non-English month abbreviations with Carbon is totally doable, and you have a couple of solid approaches depending on whether you want to leverage Carbon's built-in locale support or use your existing mapping for precise control. Here's how to tackle both:
Approach 1: Use Carbon's Locale-Specific Parsing
Carbon has built-in support for locales, so if you set the right locale before parsing, it can recognize localized month names (both full and abbreviated) automatically.
First, make sure your server has the required locale files installed. For example, on Ubuntu/Debian, you can install them with:
sudo apt-get install language-pack-de language-pack-fr language-pack-es
Then, set the Carbon locale dynamically before parsing:
Example for German:
use Carbon\Carbon; // Set German locale Carbon::setLocale('de'); // Parse the German month name (works for both abbreviations like 'Jan' and full names like 'März') $date = Carbon::parse('März'); echo $date->month; // Outputs 3 // For abbreviations like 'Okt' (German October) $octoberDate = Carbon::parse('Okt'); echo $octoberDate->month; // Outputs 10
Example for Spanish:
Carbon::setLocale('es'); $date = Carbon::parse('dic'); // Spanish abbreviation for December echo $date->month; // Outputs 12
Example for French:
Carbon::setLocale('fr'); $date = Carbon::parse('avr'); // French abbreviation for April echo $date->month; // Outputs 4
Approach 2: Use Your Existing Mapping for Exact Control
Since you already have a predefined mapping of localized month names to English abbreviations, you can use this to either directly get the month number or convert to English first then parse with Carbon.
Option A: Map Directly to Month Numbers
Adjust your array to map each localized month to its numeric value for efficiency:
$monthToNumber = [ 'de' => [ 'Jan' => 1, 'Feb' => 2, 'März' => 3, 'Apr' => 4, 'Mai' => 5, 'Juni' => 6, 'Juli' => 7, 'Aug' => 8, 'Sep' => 9, 'Okt' => 10, 'Nov' => 11, 'Dez' => 12 ], 'es' => [ 'ene' => 1, 'feb' => 2, 'mar' => 3, 'abr' => 4, 'may' => 5, 'jun' => 6, 'jul' => 7, 'ago' => 8, 'sept' => 9, 'oct' => 10, 'nov' => 11, 'dic' => 12 ], 'fr' => [ 'janv' => 1, 'Feb' => 2, 'mars' => 3, 'avr' => 4, 'mai' => 5, 'juin' => 6, 'juil' => 7, 'aout' => 8, 'sept' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12 ] ]; // Helper function to get month number function getMonthNum(string $localMonth, string $locale): ?int { global $monthToNumber; return $monthToNumber[$locale][$localMonth] ?? null; } // Usage echo getMonthNum('dic', 'es'); // 12 echo getMonthNum('avr', 'fr'); // 4
If you need a Carbon date from this number:
$monthNum = getMonthNum('März', 'de'); $carbonDate = Carbon::create(null, $monthNum); // Creates date with that month (defaults to current year, day 1)
Option B: Convert to English Abbreviation First
Use your original mapping to translate the localized month to English, then parse with Carbon as you already do:
$localToEnglish = [ 'de' => ['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' => ['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' => ['janv' => 'Jan', 'Feb' => 'Feb', 'mars' => 'Mar', 'avr' => 'Apr', 'mai' => 'May', 'juin' => 'Jun', 'juil' => 'Jul', 'aout' => 'Aug', 'sept' => 'Sep', 'oct' => 'Oct', 'nov' => 'Nov', 'dec' => 'Dec'] ]; function parseLocalMonth(string $localMonth, string $locale): ?Carbon { global $localToEnglish; $englishAbbr = $localToEnglish[$locale][$localMonth] ?? null; return $englishAbbr ? Carbon::parse($englishAbbr) : null; } // Usage $date = parseLocalMonth('dic', 'es'); echo $date->month; // 12
Which Approach to Choose?
- Use Approach 1 if you want to keep things clean and rely on Carbon's maintained locale data (great if your localized months follow standard abbreviations/full names).
- Use Approach 2 if your mappings have non-standard abbreviations that Carbon might not recognize, or if you need full control over exactly which strings map to which months.
内容的提问来源于stack exchange,提问作者Jeremy

