Last updated November 20, 2011. Created on February 3, 2009.
Edited by KarenS, smk-ka, sun. Log in to edit this page.

Overview of Date API changes in 2.x

  1. Date conversions
  2. FAPI: Date selection form element
  3. FAPI: Converting submitted date form element values
  4. date_gm*() compatibility functions
  5. SQL generation

Date conversions

The existing functions for converting date formats has been merged into one central function for date conversions.

Date 1.x:

<?php
$timestamp
= date_iso2unix($node->field_date[0]['value']);
?>

Date 2.x:

<?php
$timestamp
= date_convert($node->field_date[0]['value'], DATE_ISO, DATE_UNIX);
?>

Available date format constants can be found at the beginning of date_api.module or in date_convert() itself.

FAPI: Date selection form element

The previous function for generating a form API element has been replaced with the real FAPI element type #date_select.

Date 1.x:

<?php
  $params
= array(
   
'label' => t('Migration date'),
   
'value' => variable_get('migration_date', ''),
   
'format' => DATE_UNIX,
   
'granularity' => array('D', 'M', 'Y'),
   
'select_day' => TRUE,
   
'select_month' => TRUE,
   
'select_year' => TRUE,
   
'years_back' => 1,
   
'blank_default' => 1,
  );
 
$form['migration_date'] = date_select_input($params);
?>

Date 2.x:

<?php
  $migration
= variable_get('migration_date', '');
 
$form['migration_date'] = array(
   
'#type' => 'date_select',
   
'#title' => t('Migration date'),
   
'#default_value' => $migration ? date_convert($migration, DATE_UNIX, DATE_DATETIME) : '',
   
'#date_format' => 'd.m.Y',
   
'#date_year_range' => '-1:0',
  );
?>

FAPI: Converting submitted date form element values

Submitted form values can be converted with date_format() now.

Date 1.x:

<?php
  $migration_date
= date_array2unix($form_state['values']['migration_date']);
?>

Date 2.x:

<?php
  $migration_date
= date_convert($form_state['values']['migration_date'], DATE_ARRAY, DATE_UNIX);
?>

date_gm*() compatibility functions

The previously used compatibility functions are no longer required in PHP5 and can be replaced by native calls to gm_*() functions.

Note: The exception to this are dates exceeding the UNIX-epoch (prior 1970 and later than 2038). For such dates, it is still recommended to include and use the compatibility functions:

<?php
  module_load_include
('inc', 'date_php4');
?>

Otherwise, Date 1.x:

<?php
  $date
= date_gmmktime(array('year' => date('Y'), 'mon' => date('m'), 'mday' => date('d') + $interval));
?>

Date 2.x:

<?php
  $date
= gmmktime(0, 0, 0, $mon, $day + $interval, $year);
?>

SQL generation

The third parameter, $date_type, now takes a constant value:

Date 1.x:

<?php
  $release_date
= date_sql('DATE', 'ct.field_release_date_value', 'iso');
?>

Date 2.x:

<?php
  $release_date
= date_sql('DATE', 'ct.field_release_date_value', DATE_ISO);
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.