You are here

function calendar_systems_format_date in Calendar Systems 7.3

Same name and namespace in other branches
  1. 8 calendar_systems.module \calendar_systems_format_date()
  2. 5 calendar_systems.module \calendar_systems_format_date()
  3. 6.3 calendar_systems.module \calendar_systems_format_date()
  4. 6 calendar_systems.module \calendar_systems_format_date()
  5. 6.2 calendar_systems.module \calendar_systems_format_date()
  6. 7 calendar_systems.module \calendar_systems_format_date()
  7. 7.2 calendar_systems.module \calendar_systems_format_date()

Implementation of hook format_date

Format a date with the given configured format or a custom format string.

Drupal allows administrators to select formatting strings for 'small', 'medium' and 'large' date formats. This function can handle these formats, as well as any custom format.

Parameters

$timestamp: The exact date to format, as a UNIX timestamp.

$type: The format to use. Can be "small", "medium" or "large" for the preconfigured date formats. If "custom" is specified, then $format is required as well.

$format: A PHP date format string as required by date(). A backslash should be used before a character to avoid interpreting the character as part of a date format.

$timezone: Time zone offset in seconds; if omitted, the user's time zone is used.

$langcode: Optional language code to translate to a language other than what is used to display the page.

Return value

A translated date string in the requested format.

File

./calendar_systems.module, line 121
Contains Calendar Systems hook implementations and helpers.

Code

function calendar_systems_format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
  $calendar = calendar_systems_get_calendar_instance();

  /*
  if (!is_null($timezone)) {
    $hour=floor($timezone/3600);
    $minute=floor(($timezone%3600)/60);
    $timezone=$hour.':'.$minute;
    $calendar->setTimeZoneOffset('0:0');
  }
  */
  $calendar
    ->setTimeZoneOffset('0:0');
  if (!isset($timezone)) {
    global $user;
    if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
      $timezone = $user->timezone;
    }
    else {
      $timezone = variable_get('date_default_timezone', 0);
    }
  }
  if (!is_null($timezone)) {

    //$calendar->setTimeZoneOffset($timezone);
  }
  $timestamp += $timezone;
  switch ($type) {
    case 'small':
      $format = variable_get('date_format_short', 'm/d/Y - H:i');
      break;
    case 'large':
      $format = variable_get('date_format_long', 'l, F j, Y - H:i');
      break;
    case 'custom':

      // No change to format.
      break;
    case 'medium':
    default:
      $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
  }
  $max = strlen($format);
  $date = '';
  for ($i = 0; $i < $max; $i++) {
    $c = $format[$i];
    if (strpos('AaDlM', $c) !== FALSE) {
      $date .= t($calendar
        ->timestampToStr($c, $timestamp), array(), array(
        'langcode' => $langcode,
      ));
    }
    elseif ($c == 'F') {

      // Special treatment for long month names: May is both an abbreviation
      // and a full month name in English, but other languages have
      // different abbreviations.
      $date .= trim(t('!long-month-name ' . $calendar
        ->timestampToStr($c, $timestamp), array(
        '!long-month-name' => '',
      ), array(
        'langcode' => $langcode,
      )));
    }
    elseif (strpos('BdgGhHiIjLmnsStTUwWYyz', $c) !== FALSE) {
      $date .= $calendar
        ->timestampToStr($c, $timestamp);
    }
    elseif ($c == 'r') {
      $date .= calendar_systems_format_date($timestamp - $timezone, 'custom', 'D, d M Y H:i:s O', $timezone, $langcode);
    }
    elseif ($c == 'O') {
      $date .= sprintf('%s%02d%02d', $timezone < 0 ? '-' : '+', abs($timezone / 3600), abs($timezone % 3600) / 60);
    }
    elseif ($c == 'Z') {
      $date .= $timezone;
    }
    elseif ($c == '\\') {
      $date .= $format[++$i];
    }
    else {
      $date .= $c;
    }
  }
  return $date;
}