You are here

function token_format_date in Token 6

A copy of format_date() that supports the 'N' date format character.

See also

format_date()

1 call to token_format_date()
token_get_date_token_values in ./token.module
Build a list of common date tokens for use in hook_token_values().

File

./token.module, line 559
The Token API module.

Code

function token_format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
  global $user;
  static $timezones = array();

  // Statically cache each user's timezone so it doesn't need to be re-fetched
  // ever call.
  if (!isset($timezones[$user->uid])) {
    if (!empty($user->uid) && variable_get('configurable_timezones', 1) && strlen($user->timezone)) {
      $timezones[$user->uid] = $user->timezone;
    }
    else {
      $timezones[$user->uid] = variable_get('date_default_timezone', 0);
    }
  }
  $timestamp += $timezones[$user->uid];
  switch ($type) {
    case 'custom':

      // No change to format.
      break;
    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 '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(gmdate($c, $timestamp), array(), $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 ' . gmdate($c, $timestamp), array(
        '!long-month-name' => '',
      ), $langcode));
    }
    elseif (strpos('BdgGhHiIjLmnNsStTUwWYyz', $c) !== FALSE) {

      // This condition was modified to allow the 'N' date format character.
      $date .= gmdate($c, $timestamp);
    }
    elseif ($c == 'r') {
      $date .= token_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;
}