Created
October 16, 2015 22:25
-
-
Save sam-higton/c82c9049b0e398d845d3 to your computer and use it in GitHub Desktop.
You can now localize DateTime::format()
This file contains 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 BetterDateTime extends DateTime { | |
private $dictFormatToStrf = array ( | |
"d" => "%d", | |
"D" => "%a", | |
"j" => "_nt-dm_", //%e | |
"l" => "%A", | |
"N" => "%u", | |
"S" => "_suffix_", | |
"w" => "%w", | |
"z" => "_nt-dy_", //%j | |
"W" => "%W", //%W | |
"F" => "%B", | |
"m" => "%m", | |
"M" => "%h", | |
"n" => "_nt-m_", | |
"t" => "_daysinmonth_", | |
"L" => "_leapyear_", | |
"o" => "_prevweekyear_", | |
"Y" => "%Y", | |
"y" => "%y", | |
"a" => "%p", | |
"A" => "%P", | |
"B" => "_swatch_", | |
"g" => "%l", | |
"G" => "%k", | |
"h" => "%I", | |
"H" => "%H", | |
"i" => "%M", | |
"s" => "%S", | |
"u" => "_microseconds_", | |
"e" => "%Z", | |
"I" => "_dst_", | |
"O" => "%z", | |
"P" => "_zwithcolon_", | |
"T" => "_abbrtimezone_", | |
"Z" => "_zinseconds_", | |
"c" => "%Y-%m-%dT%H:%M:%S_zwithcolon_", | |
"r" => "%a, _nt-dm_ %b %Y %H:%M:%S %z", | |
"U" => "_unix_" | |
); | |
private $notSupported = array( | |
"_nt-dm_", | |
"_suffix_", | |
"_nt-dy_", | |
"_nt-m_", | |
"_daysinmonth_", | |
"_leapyear_", | |
"_prevweekyear_", | |
"_swatch_", | |
"_microseconds_", | |
"_dst_", | |
"_zwithcolon_", | |
"_abbrtimezone_", | |
"_zinseconds_", | |
"_unix_" | |
); | |
private function _formatToStrf ($format) { | |
$formatComponents = str_split($format); | |
$newComponents = array(); | |
$prevComponent = ""; | |
foreach($formatComponents as $component) { | |
if (array_key_exists($component, $this->dictFormatToStrf) && $prevComponent != "\\") { | |
array_push($newComponents, $this->dictFormatToStrf[$component]); | |
} else { | |
array_push($newComponents, $component); | |
} | |
$prevComponent = $component; | |
} | |
$newFormat = implode("",$newComponents); | |
return str_replace("\\","",$newFormat); | |
} | |
public function localizedFormat ($format, $locale = false) { | |
$newFormat = $this->_formatToStrf($format); | |
if($locale) { | |
$prevLocale = setlocale(LC_TIME, '0'); | |
setLocale(LC_TIME,$locale); | |
} | |
$dateString = strftime($newFormat,$this->getTimestamp()); | |
if($locale) { | |
setLocale(LC_TIME,$prevLocale); | |
} | |
foreach($this->notSupported as $target) { | |
$dateString = str_replace($target, $this->format(array_search($target,$this->dictFormatToStrf)),$dateString); | |
} | |
return $dateString; | |
} | |
} | |
$testDate = new BetterDateTime(); | |
echo $testDate->format('D j F Y H:i:s') . "<br />"; | |
echo $testDate->localizedFormat('D j F Y H:i:s','en') . "<br />"; | |
echo $testDate->localizedFormat('D j F Y H:i:s','de') . "<br />"; | |
echo $testDate->localizedFormat('D j F Y H:i:s','es') . "<br />"; | |
echo $testDate->localizedFormat('D j F Y H:i:s','fr') . "<br />"; | |
echo $testDate->localizedFormat('D j F Y H:i:s','no') . "<br />"; | |
echo $testDate->localizedFormat('r','pt') . "<br />"; | |
echo $testDate->localizedFormat('c','da') . "<br />"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment