You are here

function date_formatter_process in Date 7

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. 6 date/date.module \date_formatter_process()
  5. 7.3 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 entity field item, like $entity->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 ) )

1 call to date_formatter_process()
date_field_formatter_view in ./date.field.inc
Implements hook_field_formatter_view().

File

./date.module, line 272

Code

function date_formatter_process($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display) {
  $dates = array();
  $timezone = date_default_timezone();
  if (empty($timezone)) {
    return $dates;
  }
  $granularity = date_granularity($field);
  $settings = $display['settings'];
  $field_name = $field['field_name'];
  $format = date_formatter_format($formatter, $settings, $granularity, $langcode);
  $timezone = isset($item['timezone']) ? $item['timezone'] : '';
  $timezone = date_get_timezone($field['settings']['tz_handling'], $timezone);
  $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  $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];

      // @TODO Figure out how to replace date_fuzzy_datetime() function.
      if ($field['type'] == DATE_ISO) {

        //$value = str_replace(' ', 'T', date_fuzzy_datetime($value));
      }
      $date = new DateObject($value, $timezone_db);
      $date
        ->limitGranularity($field['settings']['granularity']);
      $dates[$processed] = array();
      $dates[$processed]['db']['object'] = $date;
      $dates[$processed]['db']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
      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 doesn't need to be processed
      $dates[$processed]['formatted'] = '';
      if (is_object($date)) {
        if ($format == 'format_interval') {
          $dates[$processed]['interval'] = date_format_interval($date);
        }
        elseif ($format == 'format_calendar_day') {
          $dates[$processed]['calendar_day'] = date_format_calendar_day($date);
        }
        elseif ($format == 'U') {
          $dates[$processed]['formatted'] = date_format_date($date, 'custom', $format);
          $dates[$processed]['formatted_date'] = date_format_date($date, 'custom', $format);
          $dates[$processed]['formatted_time'] = '';
          $dates[$processed]['formatted_timezone'] = '';
        }
        elseif (!empty($format)) {
          $dates[$processed]['formatted'] = date_format_date($date, 'custom', $format);
          $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[$processed]['formatted_timezone'] = date_format_date($date, 'custom', date_limit_format($format, array(
            'timezone',
          )));
        }
      }
    }
  }
  if (empty($dates['value2'])) {
    $dates['value2'] = $dates['value'];
  }
  $date1 = $dates['value']['local']['object'];
  $date2 = $dates['value2']['local']['object'];
  $all_day = '';
  $all_day2 = '';
  if ($format != 'format_interval') {
    $all_day1 = theme('date_all_day', array(
      'field' => $field,
      'which' => 'date1',
      'date1' => $date1,
      'date2' => $date2,
      'format' => $format,
      'entity_type' => $entity_type,
      'entity' => $entity,
    ));
    $all_day2 = theme('date_all_day', array(
      'field' => $field,
      'which' => 'date2',
      'date1' => $date1,
      'date2' => $date2,
      'format' => $format,
      'entity_type' => $entity_type,
      'entity' => $entity,
    ));
  }
  if (!empty($all_day1) && $all_day1 != $dates['value']['formatted'] || !empty($all_day2) && $all_day2 != $dates['value2']['formatted']) {
    $dates['value']['formatted_time'] = theme('date_all_day_label');
    $dates['value2']['formatted_time'] = theme('date_all_day_label');
    $dates['value']['formatted'] = $all_day1;
    $dates['value2']['formatted'] = $all_day2;
  }
  $dates['format'] = $format;
  return $dates;
}