Code Snippets

Localizing the Date format

An example of this topic is shown below. For further explanation, refer to this blog post.

$custom_formats = array(
  'Y\年n\月j\日',
  'j \d\e F, Y - H:i',
  'l, j \d\e F, Y - H:i',
  'j \d\e M Y - g:ia',
);

$spanish_date_formats = array(
  array(
    'type'  => 'long',
    'format' => 'l, j \d\e F, Y - H:i',
  ),
  array(
    'type'  => 'medium',
    'format' => 'j \d\e F, Y - H:i',
  ),
  array(
    'type'  => 'short',
    'format' => 'j \d\e M Y - g:ia',
  ),
);

$german_date_formats = array(
  array(
    'type'  => 'long',
    'format' => 'j. F Y - G:i',
  ),
  array(
    'type'  => 'medium',
    'format' => 'j. F Y - G:i',
  ),
  array(
    'type'  => 'short',
    'format' => 'j. F Y - G:i',
  ),
);

$japanese_date_formats = array(
  array(
    'type'  => 'long',
    'format' => 'Y\年n\月j\日',
  ),
  array(
    'type'  => 'medium',
    'format' => 'Y\年n\月j\日',
  ),
  array(
    'type'  => 'short',
    'format' => 'Y\年n\月j\日',
  ),
);

// create custom date formats
foreach($custom_formats as $k => $_format) {
  $format           = array();
  $format['format'] = $_format;
  $format['type']   = 'custom';
  $format['locked'] = 0;
  $format['is_new'] = 1;
		
  date_format_save($format);
}

// localize date format
foreach($spanish_date_formats as $es_format) {
		
  $langcode = 'es';
  $type     = $es_format['type'];
  $format   = $es_format['format'];
  date_locale_locale_format_save($langcode, $type, $format);
}
	
foreach($german_date_formats as $de_format) {
		
  $langcode = 'de';
  $type     = $de_format['type'];
  $format   = $de_format['format'];
  date_locale_locale_format_save($langcode, $type, $format);
}
	
foreach($japanese_date_formats as $ja_format) {
		
  $langcode = 'ja';
  $type     = $ja_format['type'];
  $format   = $ja_format['format'];
  date_locale_locale_format_save($langcode, $type, $format);
}

Resource Tags: