Created
September 19, 2014 17:39
-
-
Save withremote/effb1364e5e2a6d1f4f9 to your computer and use it in GitHub Desktop.
safe_strftime
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
// ------------------------------------------------------------- | |
// Format a time, respecting the locale and local time zone, | |
// and make sure the output string is safe for UTF-8 | |
function safe_strftime($format, $time='', $gmt=0, $override_locale='') | |
{ | |
global $locale; | |
$old_locale = $locale; | |
if (!$time) | |
$time = time(); | |
# we could add some other formats here | |
if ($format == 'iso8601' or $format == 'w3cdtf') { | |
$format = '%Y-%m-%dT%H:%M:%SZ'; | |
$gmt = 1; | |
} | |
elseif ($format == 'rfc822') { | |
$format = '%a, %d %b %Y %H:%M:%S GMT'; | |
$gmt = 1; | |
$override_locale = 'en-gb'; | |
} | |
if ($override_locale) | |
getlocale($override_locale); | |
if ($format == 'since') | |
$str = since($time); | |
elseif ($gmt) | |
$str = gmstrftime($format, $time); | |
else | |
$str = strftime($format, $time + tz_offset($time)); | |
@list($lang, $charset) = explode('.', $locale); | |
if (empty($charset)) | |
$charset = 'ISO-8859-1'; | |
//elseif (IS_WIN and is_numeric($charset)) | |
elseif ((IS_WIN and is_numeric($charset)) || in_array(intval($charset), array(1251 ,1252), true)) | |
// Score -1 for consistent naming conventions | |
$charset = 'Windows-'.$charset; | |
if ($charset != 'UTF-8' and $format != 'since') { | |
$new = ''; | |
/*if (is_callable('iconv')) { | |
$new = @iconv($charset, 'UTF-8', $str); | |
}*/ | |
if(is_callable('mb_convert_encoding')) { | |
ini_set('mbstring.substitute_character', "none"); | |
$new= mb_convert_encoding($str, 'UTF-8', $charset); | |
} | |
if ($new) | |
$str = $new; | |
elseif (is_callable('utf8_encode')) | |
$str = utf8_encode($str); | |
} | |
# revert to the old locale | |
if ($override_locale) | |
$locale = setlocale(LC_ALL, $old_locale); | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment