mediatribe.net -- Drupal and Web Development

Notice: this post was last updated 3 years 51 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>';
}
?>