Using Joomla!'s Date picker

I created some custom forms recently, using DirectPHP within Joomla! 3.0. At first I used standard text fields for the date, month, and year fields, then I had to lump up the user's input into one variable, before sending it into the database. Clumsy.
So I decided to google "PHP Datepicker" and the results brought up this article on www.lovewithbug.com which indicated that I could use Joomla! Framework's own built-in calendar date picker. Fantastic!
To use Joomla!'s default datepicker, do the following:
  1. Place this code within your code
      <?php JHTML::_('behavior.calendar'); ?>
Make sure  it is above any instance you want to use the datepicker in.

    2.  The code to display it is derived thus:
          void calendar (string $valuestring $namestring $id, [string $format = '%Y-%m-%d'], [array $attribs = null])
  • string $value: The date value
  • string $name: The name of the text field
  • string $id: The id of the text field
  • string $format: The date format
  • array $attribs: Additional html attributes
Use this to display the date picker:

Then wherever you want to include the datepicker, you can call the following function

<?php 
echo JHtml::calendar('date''%Y-%m-%d');//another example below
echo JHTML::calendar('2010-02-25','cal_field_name','cal_field_id','%Y-%m-%d');
?> 


But I had one small issue.
I needed to capture the current date and time as the default value. Using "NOW()" didn't work. More research helped me out. I inserted the function, date("Y/m/d") instead, and it worked like a charm!
So, did it work for you?
BLOG COMMENTS POWERED BY DISQUS