* * This code is licensed on the the terms of the GNU General Public License * GPLv2. You can read the full license terms at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html */ if (extension_loaded('runkit')) { // Rename the built-in date() function. runkit_function_rename('date', 'builtin_date'); // Define our own new date() function (with a fall-through to built-in). runkit_function_add('date', '$format,$timestamp=NULL,$locale=NULL',' if ($timestamp == NULL) $timestamp = time(); // Only work if the locale is actually valid. if (!empty($locale)) { $_locale = setlocale(LC_TIME, 0); $locale = setlocale(LC_TIME, $locale); setlocale(LC_TIME, $_locale); } else { $locale = setlocale(LC_TIME, 0); } // We will sue the language (first 2 chars) to check for a callback fucntion that actually // returns the suffix for the ordinal number. $lang = substr($locale, 0, 2); // Only actually override if there is an S ordinal AND language AND defined prefix. if (strpos($format, "S") !== FALSE && !empty($lang) && function_exists("_date_callback_" . $lang)) { // Put in an unlikely placeholder. $format = strtr($format, array("S" => "{}")); // Make the string using the builtin function. $output = builtin_date($format, $timestamp); // Find the ordinal suffix for the current day in the current locale, then replace. $replace = array("{}" => call_user_func("_date_callback_" . $lang, builtin_date("j", $timestamp))); // Return the output with the replaced placeholder. return strtr($output, $replace); } // Default to the standard PHP built-in implementation. return builtin_date($format, $timestamp);'); // Rename the built-in date_format() function. runkit_function_rename('date_format', 'builtin_date_format'); // Define our own new date() function (with a fall-through to built-in). runkit_function_add('date_format', '$date_time,$format,$locale=NULL',' // Only work if the locale is actually valid. if (!empty($locale)) { $_locale = setlocale(LC_TIME, 0); $locale = setlocale(LC_TIME, $locale); setlocale(LC_TIME, $_locale); } else { $locale = setlocale(LC_TIME, 0); } // We will sue the language (first 2 chars) to check for a callback fucntion that actually // returns the suffix for the ordinal number. $lang = substr($locale, 0, 2); // Only actually override if there is an S ordinal AND language AND defined prefix. if (strpos($format, "S") !== FALSE && !empty($lang) && function_exists("_date_callback_" . $lang)) { // Put in an unlikely placeholder. $format = strtr($format, array("S" => "{}")); // Make the string using the builtin function. $output = builtin_date_format($date_time, $format); // Find the ordinal suffix for the current day in the current locale, then replace. $replace = array("{}" => call_user_func("_date_callback_" . $lang, builtin_date_format($date_time, "j"))); // Return the output with the replaced placeholder. return strtr($output, $replace); } // Default to the standard PHP built-in implementation. return builtin_date_format($date_time, $format);'); // // Make a few sample callback functions. // /** * Deutsch. */ function _date_callback_de($number) { return '.'; } /** * English */ function _date_callback_en($number) { if ($number >= 10 && $number <= 19) { return "th"; } else { switch ($number % 10) { case 1: return 'st'; break; case 2: return 'nd'; break; case 3: return 'rd'; break; default: return 'th'; break; } } } /** * Suomi. */ function _date_callback_fi($number) { return '.'; } /** * Français. */ function _date_callback_fr($number) { switch ($number % 10) { case 1: return 're'; break; default: return 'e'; break; } } /** * Nederlands. */ function _date_callback_nl($number) { return 'e'; } } // endif extension_loaded()