How to add a date popup calendar onto a custom form
Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.
How to use a date popup calendar within your custom module.
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 - http://php.net/manual/en/function.date.php '#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()), );
Questions? Comments? I’m @opdavies on Twitter.
About the Author
Oliver Davies is a Full Stack Web Developer and System Administrator based in the UK. He is a Senior Software Engineer and Technical Team Lead at Inviqa and a part-time freelancer specialising in Drupal, Symfony and Laravel development and Linux systems administration.