How to add a date popup calendar onto a custom form

Posted: 23rd May 2012

Tags: forms, form-api, date, calendar, drupal-7, drupal-planet, drupal

First, I need to download the Date module, and make my module dependent on date_popup by adding the following line into my module's .info file.

dependencies[] = date_popup

Within my form builder function:

$form['date'] = array(
  '#title' => t('Arrival date'),

  // Provided by the date_popup module
  '#type' => 'date_popup',

  // Uses the PHP date() format -
  '#date_format' => 'j F Y',

  // Limits the year range to the next two upcoming years
  '#date_year_range' => '0:+2',

  // Default value must be in 'Y-m-d' format.
  '#default_value' => date('Y-m-d', time()),

About the Author

Picture of Oliver

Oliver Davies is a Web Developer, System Administrator and Drupal specialist based in the UK. He is a Senior Drupal Developer at Microserve and also provides freelance consultancy services for Drupal websites, PHP applications and Linux servers.


Currently have limited part-time capacity

Currently no spare full-time capacity.