form_date()

function form_date(string $name, ?string $value = null, array $attributes = []): string

Description

Generates an HTML date input field with a native browser date picker. The input always submits dates in ISO 8601 format (YYYY-MM-DD), regardless of how the browser displays the date to the user.

Demonstration

Below is the native HTML5 date input rendered by form_date():

HTML5 Date Format: The value must always be in YYYY-MM-DD format. Browsers display dates according to the user's locale, but your PHP code always receives predictable ISO 8601 format.

Parameters

ParameterTypeDescriptionDefaultRequired
$name string The name attribute for the input element N/A Yes
$value string|null The date value in YYYY-MM-DD format null No
$attributes array Additional HTML attributes as key-value pairs [] No

Return Value

TypeDescription
stringThe generated HTML date input element

Understanding the Attributes Array

The $attributes array accepts standard HTML input attributes. For date inputs, these are particularly useful:

AttributeDescriptionExample Value
min Earliest selectable date (YYYY-MM-DD) '2025-01-01'
max Latest selectable date (YYYY-MM-DD) '2025-12-31'
required Field must have a value ['required' => true]
readonly Prevent user editing ['readonly' => true]
disabled Disable the input ['disabled' => true]

Boolean Attributes: Pass true for boolean attributes like required, readonly, or disabled. This generates clean HTML5 syntax: <input required>

Example #1: Basic Date Input

PHP
echo form_date('birthday');
HTML
Output: <input type="date" name="birthday">

The example above shows the simplest usage. This generates a date input field where users can select any date using their browser's native date picker.

Example #2: Date Input with Pre-set Value

PHP
public function create(): void {
    // Pre-fill with today's date for new records
    $data['event_date'] = date('Y-m-d');
    $data['form_location'] = BASE_URL . 'events/submit';
    $this->view('event_form', $data);
}

View file:

View File
<?php
echo form_open($form_location);
echo form_label('Event Date');
echo form_date('event_date', $event_date);
echo form_submit('submit', 'Save Event');
echo form_close();
?>

The example above demonstrates setting a default value. When creating new records, it's common to pre-fill date fields with today's date or another sensible default.

Form Repopulation: When a form fails validation, use post('field_name', true) in the controller to fetch the submitted value and pass it back to the view for repopulation.

Example #3: Date Input with Business Constraints

PHP
public function create(): void {
    $data['booking_date'] = post('booking_date', true);
    
    // Business rule: bookings can only be made 1-30 days in advance
    $data['min_date'] = date('Y-m-d', strtotime('+1 day'));
    $data['max_date'] = date('Y-m-d', strtotime('+30 days'));
    
    $data['form_location'] = BASE_URL . 'bookings/submit';
    $this->view('booking_form', $data);
}

public function submit(): void {
    $this->validation->set_rules('booking_date', 'booking date', 'required|valid_date');
    
    if ($this->validation->run() === true) {
        $data['customer_name'] = post('customer_name', true);
        $data['booking_date'] = post('booking_date', true);
        
        $this->db->insert($data, 'bookings');
        set_flashdata('Booking confirmed successfully');
        redirect('bookings/confirm');
    } else {
        $this->create();
    }
}

View file:

View File
<?php
echo form_open($form_location);
echo form_label('Booking Date');
$attributes = [
    'min' => $min_date,
    'max' => $max_date,
    'required' => true
];
echo form_date('booking_date', $booking_date, $attributes);
echo form_submit('submit', 'Confirm Booking');
echo form_close();
?>

The example above shows how to enforce business rules using min and max attributes. The browser prevents users from selecting dates outside the allowed range.

Server-side Validation Required: Always validate date inputs server-side with the valid_date rule. Browser validation can be bypassed by malicious users.

Example #4: Complete Create/Update Workflow

PHP
public function create(): void {
    $update_id = segment(3, 'int');
    
    if ($update_id > 0 && REQUEST_TYPE === 'GET') {
        // Editing existing record - load from database
        $record = $this->db->get_where($update_id, 'tasks');
        $data['task_name'] = $record->task_name;
        $data['due_date'] = $record->due_date;
    } else {
        // New record or validation error - use POST data
        $data['task_name'] = post('task_name', true);
        $data['due_date'] = post('due_date', true);
    }
    
    $data['form_location'] = BASE_URL . 'tasks/submit/' . $update_id;
    $this->view('task_form', $data);
}

public function submit(): void {
    $update_id = segment(3, 'int');
    
    $this->validation->set_rules('task_name', 'task name', 'required|min_length[3]');
    $this->validation->set_rules('due_date', 'due date', 'required|valid_date');
    
    if ($this->validation->run() === true) {
        $data['task_name'] = post('task_name', true);
        $data['due_date'] = post('due_date', true);
        
        if ($update_id > 0) {
            $this->db->update($update_id, $data, 'tasks');
            set_flashdata('Task updated successfully');
        } else {
            $this->db->insert($data, 'tasks');
            set_flashdata('Task created successfully');
        }
        
        redirect('tasks/manage');
    } else {
        $this->create();
    }
}

View file:

View File
<?php
echo form_open($form_location);
echo form_label('Task Name');
echo form_input('task_name', $task_name);

echo form_label('Due Date');
$attributes = [
    'min' => date('Y-m-d'), // Can't set due dates in the past
    'required' => true
];
echo form_date('due_date', $due_date, $attributes);

echo form_submit('submit', 'Save Task');
echo form_close();
?>

The example above demonstrates the complete create/update pattern. Notice that segment(3, 'int') is properly type-cast, and the form works for both creating new records and editing existing ones.

Database Compatibility: MySQL's DATE type uses YYYY-MM-DD format, which matches HTML5 date inputs exactly. No date format conversion is needed between the form and database.