2015-03-16 21:18:03 +00:00
---
title: How to add a date popup calendar onto a custom form
2018-06-02 10:58:39 +00:00
date: '2012-05-23'
2015-03-16 21:18:03 +00:00
slug: add-date-popup-calendar-custom-form
tags:
2015-06-14 02:27:41 +00:00
- forms
- form-api
- date
- calendar
- drupal-7
- drupal-planet
- drupal
2016-12-29 16:32:52 +00:00
use: [posts]
2015-03-16 21:18:03 +00:00
---
2015-06-18 07:58:56 +00:00
{% block excerpt %}
How to use a date popup calendar within your custom module.
{% endblock %}
{% block content %}
2015-03-18 21:16:06 +00:00
First, I need to download the [Date ](http://drupal.org/project/date "Date module on Drupal.org" ) module, and make my module dependent on date_popup by adding the following line into my module's .info file.
2015-03-16 21:18:03 +00:00
2017-03-16 08:09:52 +00:00
```language-ini
dependencies[] = date_popup
```
2015-03-16 21:18:03 +00:00
Within my form builder function:
2017-03-16 08:09:52 +00:00
```language-php
2015-03-16 21:18:03 +00:00
$form['date'] = array(
2015-06-14 02:27:41 +00:00
'#title' => t('Arrival date'),
2015-03-16 21:18:03 +00:00
2015-06-14 02:27:41 +00:00
// Provided by the date_popup module
'#type' => 'date_popup',
2015-03-16 21:18:03 +00:00
2015-06-14 02:27:41 +00:00
// Uses the PHP date() format - http://php.net/manual/en/function.date.php
'#date_format' => 'j F Y',
2015-03-16 21:18:03 +00:00
2015-06-14 02:27:41 +00:00
// Limits the year range to the next two upcoming years
'#date_year_range' => '0:+2',
2015-03-16 21:18:03 +00:00
2015-06-14 02:27:41 +00:00
// Default value must be in 'Y-m-d' format.
'#default_value' => date('Y-m-d', time()),
2015-03-16 21:18:03 +00:00
);
2017-03-16 08:09:52 +00:00
```
2015-06-18 07:58:56 +00:00
{% endblock %}