Date and Time Localisation in wordpress

The Ical Events Plugin settings page now offers 4 date localisation options, with the current date and time displayed using that function and your wordpress installation’s timezone.  After choosing a function you should still check your events are displaying at the correct time.

  • none – Thu, 28 Jan 2010 06:15:57 +0200
  • amr – Thu, 28 Jan 2010 06:15:57 +0200
  • wp – Thu, 28 Jan 2010 06:15:57 +0200
  • wpgmt – Thu, 28 Jan 2010 04:15:57 +0000

Explanation of each option:

  • None – will not apply any localisation – this is the new default for any website with en-US in their WPLANG setting.
  • amr – will apply a datetime object based version of the wordpress date_i18n function.  Default for non english sites.  This is described in more detail below.
  • wp – will apply the wordpress date_i18n function with the gmt flag set to false
  • wpgmt –  will apply the wordpress date_i18n function with the gmt flag set to true

Background

WordPress does not actually set the locale in PHP for websites in other languages.  This is for good reason it seems, the locale settings can mess with the use of numbers for other purposes.

So one sets WPLANG  in the config file or via a plugin, as described in wordpress, and then hope that all the plugin authors know to use the wordpress localisation functions date_i18n.

date_i18n( 'D F j G:i:s T Y', strtotime( $datetimeobject ) ) or
date_i18n( 'D F j G:i:s T Y', $unixtimestamp )

I have had some strange experiences in using this function with my ical plugins. In certain unusual situations, use of the function returned unexpected results. I tried preparing the unix timestamp in different ways (with/without the offset), and passing gmt=true or false to the function, and while each change seemed to solve some persons anomalies, it seemed to create a weirdness for someone else.  Since mostly things were fine on my servers, it was difficult to determine what  situations caused the anomalies in others.

It seemed to happen possibly where there are multiple timezones involved (SERVER, WORDPRESS, ICAL FILE? EVENT?) and possibly different php versions – I have not been able to pin it down completely.

I was also concerned about having to convert back to a unix timestamp since all my other date work was kept using the datetime class.

The date_i18n function probably used the unix timestamp for compatibility reasons, but there was no reason why the plugin had to be restricted by that. I therefore wrote a datetime object based version, maintaining the ability to filter for non typical languages.  This appeared to stabilise some of the wierd differences between servers and wp installs:

Download file with function: amr_date_i18n

if (!function_exists('amr_date_i18n')) {
function amr_date_i18n( $dateformatstring, $dateobj = null) {
	global $wp_locale;
	// store original value for language with untypical grammars
	// see http://core.trac.wordpress.org/ticket/9396
	$req_format = $dateformatstring;
	$datefunc = 'date';
	if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
		$datemonth = $wp_locale->get_month( $dateobj->format('m') );
		$datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
		$dateweekday = $wp_locale->get_weekday( $dateobj->format('w') );
		$dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
		$datemeridiem = $wp_locale->get_meridiem( $dateobj->format('a') );
		$datemeridiem_capital = $wp_locale->get_meridiem( $dateobj->format( 'A') );
		$dateformatstring = ' '.$dateformatstring;
		$dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
		$dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
		$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
	}
	$j = $dateobj->format( $dateformatstring );
	// allow plugins to redo this entirely for languages with untypical grammars
	$j = apply_filters('amr_date_i18n', $j, $req_format, $dateobj );
	return $j;
}
}

About anmari

the developer at anmari
This entry was posted in Notes and tagged , . Bookmark the permalink.