function date_format_date in Date 6.2
Same name and namespace in other branches
- 5.2 date_api.module \date_format_date()
- 5 date.inc \date_format_date()
- 6 date_api.module \date_format_date()
- 7.3 date_api/date_api.module \date_format_date()
- 7 date_api/date_api.module \date_format_date()
- 7.2 date_api/date_api.module \date_format_date()
Reworked from Drupal's format_date function to handle pre-1970 and post-2038 dates and accept a date object instead of a timestamp as input.
Translates formatted date results, unlike PHP function date_format().
Parameters
$date: A date object, could be created by date_make_date().
$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.
Return value
A translated date string in the requested format.
23 calls to date_format_date()
- DateAPITestCase::testDateAPI in tests/
date_api.test - date_api_add_date_formats_form in ./
date_api.admin.inc - Allow users to add additional date formats.
- date_api_argument_handler::summary_argument in includes/
date_api_argument_handler.inc - Provide the argument to use to link from the summary to the next level; this will be called once per row of a summary, and used as part of $view->get_url().
- date_api_argument_handler::summary_name in includes/
date_api_argument_handler.inc - Provide a link to the next level of the view from the summary.
- date_api_argument_handler::title in includes/
date_api_argument_handler.inc - Provide a link to the next level of the view from the argument.
File
- ./
date_api.module, line 714 - This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.
Code
function date_format_date($date, $type = 'medium', $format = '', $langcode = NULL) {
if (empty($date)) {
return '';
}
if (function_exists('timezone_name_from_abbr') && get_class($date) != 'DateTime') {
$date = date_make_date($date);
}
switch ($type) {
case 'small':
case 'short':
$format = variable_get('date_format_short', 'm/d/Y - H:i');
break;
case 'large':
case 'long':
$format = variable_get('date_format_long', 'l, F j, Y - H:i');
break;
case 'custom':
$format = $format;
break;
case 'medium':
default:
$format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
}
$max = drupal_strlen($format);
$datestring = '';
for ($i = 0; $i < $max; $i++) {
$c = $format[$i];
switch ($c) {
// Use date_t() for ambiguous short strings that need translation.
// We send long day and month names to date_t(), along with context.
case 'l':
$datestring .= date_t(date_format($date, 'l'), 'day_name', $langcode);
break;
case 'D':
$datestring .= date_t(date_format($date, 'l'), 'day_abbr', $langcode);
break;
case 'F':
$datestring .= date_t(date_format($date, 'F'), 'month_name', $langcode);
break;
case 'M':
$datestring .= date_t(date_format($date, 'F'), 'month_abbr', $langcode);
break;
case 'A':
case 'a':
$datestring .= date_t(date_format($date, $c), 'ampm', $langcode);
break;
// The timezone name translations can use t().
case 'e':
case 'T':
$datestring .= t(date_format($date, $c));
break;
// Remaining date parts need no translation.
case 'O':
$datestring .= sprintf('%s%02d%02d', date_offset_get($date) < 0 ? '-' : '+', abs(date_offset_get($date) / 3600), abs(date_offset_get($date) % 3600) / 60);
break;
case 'P':
$datestring .= sprintf('%s%02d:%02d', date_offset_get($date) < 0 ? '-' : '+', abs(date_offset_get($date) / 3600), abs(date_offset_get($date) % 3600) / 60);
break;
case 'Z':
$datestring .= date_offset_get($date);
break;
case '\\':
$datestring .= $format[++$i];
break;
case 'r':
$datestring .= date_format_date($date, 'custom', 'D, d M Y H:i:s O', $langcode);
break;
default:
if (strpos('BdcgGhHiIjLmnNosStTuUwWYyz', $c) !== FALSE) {
$datestring .= date_format($date, $c);
}
else {
$datestring .= $c;
}
}
}
return $datestring;
}