You are here

function date_formatter_process in Date 6

Same name and namespace in other branches
  1. 8 date.module \date_formatter_process()
  2. 5.2 date/date.module \date_formatter_process()
  3. 6.2 date/date.module \date_formatter_process()
  4. 7.3 date.module \date_formatter_process()
  5. 7 date.module \date_formatter_process()
  6. 7.2 date.module \date_formatter_process()

Helper function for creating formatted date arrays from a formatter.

Use the Date API to get an object representation of a date field

Parameters

array $field:

array $item - a node field item, like $node->myfield[0]:

Return value

array that holds the From and To date objects Each date object looks like: date [value] => array ( [db] => array ( // the value stored in the database [object] => the datetime object [datetime] => 2007-02-15 20:00:00 ) [local] => array ( // the local representation of that value [object] => the datetime object [datetime] => 2007-02-15 14:00:00 [timezone] => US/Central [offset] => -21600 ) )

2 calls to date_formatter_process()
theme_date_display_combination in date/date.theme
Theme from/to date combination in the view.
theme_date_format_interval in date/date.theme
Theme a format interval for a date element

File

date/date.module, line 176
Defines date/time field types for the Content Construction Kit (CCK).

Code

function date_formatter_process($element) {
  $dates = array();
  $timezone = date_default_timezone_name();
  if (empty($timezone)) {
    return $dates;
  }
  $field_name = $element['#field_name'];
  $fields = content_fields();
  $field = $fields[$field_name];
  $formatter = $element['#formatter'];
  $format = date_formatter_format($formatter, $field_name);
  $item = $element['#item'];
  $timezone = isset($item['timezone']) ? $item['timezone'] : '';
  $process = date_process_values($field);
  foreach ($process as $processed) {
    if (empty($item[$processed])) {
      $dates[$processed] = NULL;
    }
    else {

      // create a date object with a gmt timezone from the database value
      $value = $item[$processed];
      if ($field['type'] == DATE_ISO) {
        $value = date_fuzzy_datetime($value);
      }
      $date = date_make_date($value, 'UTC', $field['type']);
      $dates[$processed] = array();
      $dates[$processed]['db']['object'] = $date;
      $dates[$processed]['db']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);

      // For no timezone handling, set local value to the same as the db value.
      if (!date_timezone_convert($field, $item[$processed])) {
        $dates[$processed]['local'] = $dates[$processed]['db'];
      }
      else {
        $timezone = date_get_timezone($field['tz_handling'], $timezone);
        date_timezone_set($date, timezone_open($timezone));
        $dates[$processed]['local']['object'] = $date;
        $dates[$processed]['local']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
        $dates[$processed]['local']['timezone'] = $timezone;
        $dates[$processed]['local']['offset'] = date_offset_get($date);
      }

      //format the date, special casing the 'interval' format which doesnt need to be processed
      $dates[$processed]['formatted'] = '';
      if (is_object($date)) {
        if ($format == 'format_interval') {
          $dates[$processed]['interval'] = date_format_interval($date);
        }
        elseif (!empty($format)) {
          $dates[$processed]['formatted'] = date_format_date($date, 'custom', $format) . $append;
          $dates[$processed]['formatted_date'] = date_format_date($date, 'custom', date_limit_format($format, array(
            'year',
            'month',
            'day',
          )));
          $dates[$processed]['formatted_time'] = date_format_date($date, 'custom', date_limit_format($format, array(
            'hour',
            'minute',
            'second',
          )));
        }
      }
    }
  }
  $dates['format'] = $format;
  return $dates;
}