Alternative approach to #243129. From: damz --- common.inc | 57 +++++++++++++++++++++++++++++++++------------------------ 1 files changed, 33 insertions(+), 24 deletions(-) diff --git includes/common.inc includes/common.inc index dced29c..5e19ce3 100644 --- includes/common.inc +++ includes/common.inc @@ -1916,32 +1916,41 @@ function format_date($timestamp, $type = 'medium', $format = '', $timezone = NUL // Set the time zone for the DateTime object. date_timezone_set($date_time, $timezones[$timezone]); - for ($i = 0; $i < $max; $i++) { - $c = $format[$i]; - if (strpos('AaeDlMT', $c) !== FALSE) { - $date .= t(date_format($date_time, $c), array(), array('langcode' => $langcode)); - } - elseif ($c == 'F') { - // Special treatment for long month names: May is both an abbreviation - // and a full month name in English, but other languages have - // different abbreviations. - $date .= t(date_format($date_time, $c), array(), array('context' => 'Long month name', 'langcode' => $langcode)); - } - elseif (strpos('BcdGgHhIijLmNnOoPSstUuWwYyZz', $c) !== FALSE) { - $date .= date_format($date_time, $c); - } - elseif ($c == 'r') { - $date .= format_date($timestamp, 'custom', 'D, d M Y H:i:s O', $timezone, $langcode); - } - elseif ($c == '\\') { - $date .= $format[++$i]; - } - else { - $date .= $c; - } + // Pass the DateTime object and the langcode to the callback function. + _format_date_callback(array(), array('date_time' => $date_time, 'langcode' => $langcode)); + + return preg_replace_callback('/(\\\?)([AaeDlMTFBcdGgHhIijLmNnOoPSstUuWwYyZzr])/', '_format_date_callback', $format); +} + +function _format_date_callback(array $matches, $options = NULL) { + static $date_time, $langcode; + + if (isset($options)) { + $date_time = $options['date_time']; + $langcode = $options['langcode']; + return; } - return $date; + list(, $backslashes, $c) = $matches; + + if (!empty($backslashes)) { + return $c; + } + elseif (strpos('AaeDlMT', $c) !== FALSE) { + return t(date_format($date_time, $c), array(), array('langcode' => $langcode)); + } + elseif ($c == 'F') { + // Special treatment for long month names: May is both an abbreviation + // and a full month name in English, but other languages have + // different abbreviations. + return t(date_format($date_time, $c), array(), array('context' => 'Long month name', 'langcode' => $langcode)); + } + elseif (strpos('BcdGgHhIijLmNnOoPSstUuWwYyZz', $c) !== FALSE) { + return date_format($date_time, $c); + } + elseif ($c == 'r') { + return format_date($timestamp, 'custom', 'D, d M Y H:i:s O', $timezone, $langcode); + } } /**