mediatribe.net -- Drupal and Web Development

Notice: this post was last updated 3 years 40 weeks ago so it might be outdated. Please be cautious before implementing any of suggestions herein.

nicely formatted multilingual dates

Here is a code snippet which allows nicely formatted multilingual dates

<?php
/**
* Generate some nicely formatted dates depending on the current lang
*/
function MYMODULE_format_date($timestamp, $lang = NULL) {
  if (!
$lang) {
    global
$language;
   
$lang = $language->language;
  }
 
$extra = '';
  switch (
$lang) {
    case
'fr':
      if (
date('j', $timestamp) == 1) {
       
$extra = 'er';
      }
      return
drupal_strtolower(format_date($timestamp, 'custom', 'j') . $extra . ' ' . t(format_date($timestamp, 'custom', 'F Y'), array(), array('context' => 'admin')));
      break;
    default:
      if (
date('j', $timestamp) == 1) {
       
$extra = 'st';
      }
      return
format_date($timestamp, 'custom', 'F j', NULL, $lang) . $extra . ', ' . format_date($timestamp, 'custom', 'Y');
      break;
  }
}
?>

The above goes in your module, and what follows goes in your template.php in your theme.

<?php
/**
* Implement theme hook suggestion date_display_single().
*/
function MYTHEME_date_display_single($variables) {
 
$date = $variables['date'];
 
$timezone = $variables['timezone'];
 
$datetime = '';
 
  if (isset(
$variables['dates']['value']['db']['datetime']) && function_exists('MYMODULE_format_date')) {
   
$timestamp = strtotime($variables['dates']['value']['db']['datetime']);
   
$date = MYMODULE_format_date($timestamp);
   
$datetime = ' datetime="' . format_date($timestamp, 'custom', 'Y-m-d') . '"';
  }
 
 
// Wrap the result with the attributes.
 
return '<time' . $datetime . '>' . $date . $timezone . '</time>';
}
?>

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.