Created
February 3, 2017 13:44
-
-
Save travisfont/941b91ebc8af42475398a824f5b5df32 to your computer and use it in GitHub Desktop.
Simple Date Localization in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class LocaleDateFormat | |
| { | |
| private $locale; | |
| private $pattern; | |
| public function __construct($pattern, $locale = 'en_US') | |
| { | |
| $this->setLocale($locale); | |
| $this->setPattern($pattern); | |
| } | |
| public function setLocale($locale) | |
| { | |
| $this->locale = $locale; | |
| } | |
| public function setPattern($pattern) | |
| { | |
| $this->pattern = $pattern; | |
| } | |
| public function localeFormat($locale, $date) | |
| { | |
| $this->setLocale($locale); | |
| return $this->format($date); | |
| } | |
| public function format($date) | |
| { | |
| $formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::FULL, IntlDateFormatter::FULL); | |
| $formatter->setPattern($this->pattern); | |
| return $formatter->format($date); | |
| } | |
| public static function formatDate($pattern, $locale = 'en_US') | |
| { | |
| $dateFormat = new LocaleDateFormat($pattern, $locale); | |
| return $dateFormat->localeFormat($locale, new DateTime()); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<?php var_dump(LocaleDateFormat::formatDate('MMMM', 'de_DE'));