You are here

function date_field_object in Date 5

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 of From and To date objects Each date object looks like: date [object] => stdClass Object ( [db] => stdClass Object ( // the value stored in the database [timestamp] => 1171569600 [iso] => 2007-02-15T20:00:00 [parts] => Array ( [seconds] => 0 [minutes] => 0 [hours] => 20 [mday] => 15 [wday] => 4 [mon] => 2 [year] => 2007 [yday] => 45 [weekday] => Thursday [month] => February [0] => 1171569600 ) ) [local] => stdClass Object ( // the local representation of that value [timestamp] => 1171548000 [iso] => 2007-02-15T14:00:00 [parts] => Array ( [seconds] => 0 [minutes] => 0 [hours] => 14 [mday] => 15 [wday] => 4 [mon] => 2 [year] => 2007 [yday] => 45 [weekday] => Thursday [month] => February [0] => 1171548000 ) [timezone] => US/Central [offset] => -21600 ) )

1 call to date_field_object()
date_field_formatter in ./date.module
Implementation of hook_field_formatter().

File

./date.module, line 490
Defines a date/time field type.

Code

function date_field_object($field, $item) {
  $dates = array();
  if (!is_array($field) || !is_array($item)) {
    return $dates;
  }
  include_once drupal_get_path('module', 'date_api') . '/date.inc';
  if ($field['todate']) {
    $process = array(
      'value',
      'value2',
    );
  }
  else {
    $process = array(
      'value',
    );
  }
  foreach ($process as $processed) {
    if (empty($item[$processed])) {
      $dates[$processed]['object'] = NULL;
    }
    else {

      // create a date object with a gmt timezone from the database value
      $date = date_make_date(trim($item[$processed]), 'GMT', 'db', $field['type']);

      // For no timezone handling, set local value to the same as the db value.
      if (date_no_conversion($date) || $field['tz_handling'] == 'none' || !in_array('H', date_granularity_array($field)) || $field['tz_handling'] == 'date' && empty($item['timezone'])) {
        date_convert_timezone($date, 'GMT', 'none', 'GMT', 'local');
      }
      else {
        date_convert_timezone($date, 'GMT', date_get_timezone($field['tz_handling'], $item['timezone']), 'local');
      }
      $dates[$processed]['object'] = $date;
    }
  }
  return $dates;
}