Skip to content
Derek Jones edited this page Jul 5, 2012 · 34 revisions

Category:Library Category:Library::Date Category:Library::Form

FormDate is a class that creates select and option tags for date and time elements. It relies on the form helpers form_dropdown function. The forum thread where it all started.

Installation

• Code Igniter version 1.5 or newer required. • Download & unzip formdate. • Put the formdate.php in your apps libraries folder. Create a libraries folder if it doesn’t exist.

Example

Load and instantiate in controller

$this->load->library('formdate');
$formdate = new FormDate();

Set options

$formdate->setLocale('nl_BE');
$formdate->year['start'] = 1950;
$formdate->year['end'] = 2006;
$formdate->month['values'] = 'numbers';

Make formdate available in the view

$data['formdate'] = $formdate;
$this->load->view('index', $data);

Display in view

<form>
<label>Name</label>
&lt;input type="text" /&gt;

<label>Day of Birth</label>
&lt;?=$formdate->selectDay()?&gt;    
&lt;?=$formdate->selectMonth()?&gt;    
&lt;?=$formdate->selectYear()?&gt;    

&lt;input type='button' /&gt;
&lt;/form&gt;

Date Validation

Date validations are easy to do with php's built in checkdate(). Put this function in a validation callback, feed it with the month, day and year from your POST array and it will return bool true if valid.

function valid_date()
{
    if (!checkdate($this->input->post('month'), $this->input->post('day'), $this->input->post('year')))
    {
        $this->validation->set_message('valid_date', 'The %s field is invalid.');
        return FALSE;
     }
}

More than one FormDate field per page

To add more than one date field per page you can use the configuration variable prefix to name them differently.

$startdate= new FormDate();
$startdate->config['prefix']="start_";
...
$enddate= new FormDate();
$enddate->config['prefix']="end_";
..

Would create date fields as: start_year, start_month, start_day and end_year, end_month, end_day

Release History

Version 0.10 (14 June 2006)

• Initial test release

Version 0.11 (17 June 2006)

• Added config prefix

Version 0.12 (26 October 2006)

• Fixed hour, minute and second selection bugs (reported by Maciej Kus) • Converted to a library since scripts are deprecated since CI 1.4.

Version 0.13 (3 April 2007)

• Added day argument value of 1 to mktime() function in selectMonth() method. This fixes wrong double month option elements. • Added config option to display blank value for day, month and year select elements.

Version 0.15 (4 April 2007)

• Blank value is always first element in dropdown list. • Auto select blank element instead of current date when blank option setted. • Added blank value for hour, minute, second and meridian • phpDocumentor style comments • Deprecated init file.

Version 0.16 (4 May 2007)

• Removed default values for referenced arguments because they are not supported under PHP4.

Clone this wiki locally