Skip to content

Instantly share code, notes, and snippets.

@travisfont
Created February 3, 2017 13:44
Show Gist options
  • Select an option

  • Save travisfont/941b91ebc8af42475398a824f5b5df32 to your computer and use it in GitHub Desktop.

Select an option

Save travisfont/941b91ebc8af42475398a824f5b5df32 to your computer and use it in GitHub Desktop.
Simple Date Localization in PHP
<?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());
}
}
@travisfont
Copy link
Author

<?php var_dump(LocaleDateFormat::formatDate('MMMM', 'de_DE'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment