--- handlers/views_handler_field_date.inc.orig	2009-06-02 20:20:18.000000000 +0200
+++ handlers/views_handler_field_date.inc	2010-06-17 13:16:05.234375000 +0200
@@ -11,6 +11,7 @@ class views_handler_field_date extends v
 
     $options['date_format'] = array('default' => 'small');
     $options['custom_date_format'] = array('default' => '');
+    $options['query_format'] = array('default' => FALSE);
 
     return $options;
   }
@@ -18,20 +19,45 @@ class views_handler_field_date extends v
   function options_form(&$form, &$form_state) {
     parent::options_form($form, $form_state);
     $time = time();
+    
+    $date_format_options = array(
+      'small' => 'small => ' . format_date($time, 'small'),
+      'medium' => 'medium => ' . format_date($time, 'medium'),
+      'large' => 'large => ' . format_date($time, 'large'),
+      'custom' => t('Custom'),
+      'raw time ago' => t('Time ago'),
+      'time ago' => t('Time ago (with "ago" appended)'),
+      'raw time span' => t('Time span (future dates start with - )'),
+      'time span' => t('Time span (with "ago/hence" appended)'),
+    );
+    if (module_exists('date_api')) {
+      // from date_api_date_format_form_elements()
+      // Get list of all available date format types.
+      $format_types = date_get_format_types('');
+      // Get list of all available date formats.
+      $all_formats = array();
+      $date_formats = date_get_formats('');
+      foreach ($date_formats as $type => $format_info) {
+        $all_formats = array_merge($all_formats, $format_info);
+      }
+      $custom_formats = date_get_formats('custom');
+      $date_format_options_date_api = array();
+      foreach ($format_types as $type => $type_info) {
+        // only for custom types
+        if ($type_info['locked'] == 0) {
+          $default = variable_get('date_format_' . $type, array_shift(array_keys($all_formats)));
+          // Get format type info for this format type.
+          $type_info = date_get_format_types($type);
+          $date_format_options_date_api[$default] = 'Date API: '. $type_info['title'] . ' => ' . format_date($time, 'custom', $default);
+        }
+      }
+      $date_format_options += $date_format_options_date_api;
+    }
 
     $form['date_format'] = array(
       '#type' => 'select',
       '#title' => t('Date format'),
-      '#options' => array(
-        'small' => format_date($time, 'small'),
-        'medium' => format_date($time, 'medium'),
-        'large' => format_date($time, 'large'),
-        'custom' => t('Custom'),
-        'raw time ago' => t('Time ago'),
-        'time ago' => t('Time ago (with "ago" appended)'),
-        'raw time span' => t('Time span (future dates start with - )'),
-        'time span' => t('Time span (with "ago/hence" appended)'),
-      ),
+      '#options' => $date_format_options,
       '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
     );
     $form['custom_date_format'] = array(
@@ -42,6 +68,34 @@ class views_handler_field_date extends v
       '#process' => array('views_process_dependency'),
       '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span')),
     );
+    $form['query_format'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Use Date Format in query'),
+      '#default_value' => $this->options['query_format'],
+      '#description' => t('Change the SQL query to use the defined date format instead standard unixtime.<br> Only valid for Custom Date Formats and Date API defined formats'),
+    );
+  }
+
+  function query() {
+    $this->ensure_my_table();
+
+    if ($this->options['query_format']) {
+      $date_format_options = $this->options;
+      $default_views_formats = array('small','medium','large','raw time ago','time ago','raw time span','time span');
+      if (!in_array($date_format_options['date_format'], $default_views_formats)) {
+        $date_format = ($date_format_options['date_format'] == 'custom') ? $date_format_options['custom_date_format'] : $date_format_options['date_format'];
+        $sql_date_format = views_date_sql_format($date_format, $this->table_alias . '.' . $this->real_field);
+        $this->field_alias = $this->query->add_field(NULL, $sql_date_format, $this->table_alias . '_' . $this->real_field);
+      }
+      else {
+        $this->field_alias = $this->query->add_field($this->table_alias, $this->real_field);
+      }
+    }
+    else {
+      $this->field_alias = $this->query->add_field($this->table_alias, $this->real_field);
+    }
+
+    $this->add_additional_fields();    
   }
 
   function render($values) {
@@ -57,6 +111,10 @@ class views_handler_field_date extends v
     else {
       $time_diff = time() - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
       switch ($format) {
+        case 'small':
+        case 'medium':
+        case 'large':
+          return format_date($value, $format);     
         case 'raw time ago':
           return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
         case 'time ago':
@@ -66,9 +124,9 @@ class views_handler_field_date extends v
         case 'time span':
           return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
         case 'custom':
-          return format_date($value, $format, $custom_format);
+          return ($this->options['query_format']) ? $value : format_date($value, 'custom', $custom_format);
         default:
-          return format_date($value, $format);
+          return ($this->options['query_format']) ? $value : format_date($value, 'custom', $format);
       }
     }
   }
