form_time()
function form_time(string $name, ?string $value = null, array $attributes = []): string
Description
Generates an HTML time input field with a native browser time picker. The input always submits time in 24-hour format (HH:MM or HH:MM:SS), regardless of how the browser displays the time to the user.
Browser Display vs Submitted Format: Browsers display times according to user locale (12-hour with AM/PM in US, 24-hour in Europe), but always submit in consistent 24-hour format that your PHP code can rely on.
Demonstration
Below is the native HTML5 time input rendered by form_time():
Parameters
| Parameter | Type | Description | Default | Required |
| $name |
string |
The name attribute for the input element |
N/A |
Yes |
| $value |
string|null |
The time value in HH:MM or HH:MM:SS format |
null |
No |
| $attributes |
array |
Additional HTML attributes as key-value pairs |
[] |
No |
Return Value
| Type | Description |
| string | The generated HTML time input element |
Understanding the Attributes Array
The $attributes array accepts standard HTML input attributes. For time inputs, these are particularly useful:
| Attribute | Description | Example Value |
min |
Earliest selectable time (HH:MM) |
'09:00' |
max |
Latest selectable time (HH:MM) |
'17:00' |
step |
Time interval in seconds |
900 (15 minutes) |
required |
Field must have a value |
['required' => true] |
readonly |
Prevent user editing |
['readonly' => true] |
disabled |
Disable the input |
['disabled' => true] |
Common Step Values:
60 - 1 minute (browser default)
300 - 5 minutes
900 - 15 minutes
1800 - 30 minutes
3600 - 1 hour
1 - 1 second (for precise timing)
Example #1: Basic Time Input
echo form_time('start_time');
Output: <input type="time" name="start_time">
The example above shows the simplest usage. This generates a time input where users can select any time using their browser's native time picker.
Example #2: Time Input with Default Value
public function create(): void {
// Set default to 9:00 AM for new bookings
$data['booking_time'] = '09:00';
$data['form_location'] = BASE_URL . 'bookings/submit';
$this->view('booking_form', $data);
}
View file:
<?php
echo form_open($form_location);
echo form_label('Booking Time');
echo form_time('booking_time', $booking_time);
echo form_submit('submit', 'Book Appointment');
echo form_close();
?>
The example above demonstrates setting a default value. Common business hours like 9:00 AM make good defaults for time inputs.
Form Repopulation: After validation errors, use post('field_name', true) in the controller to fetch the submitted value. This returns an empty string if no value was submitted, which works perfectly with form helpers.
Example #3: Appointment System with Constraints
public function create(): void {
$data['appointment_time'] = post('appointment_time', true);
// Business hours: 9 AM to 5 PM, 30-minute intervals
$data['min_time'] = '09:00';
$data['max_time'] = '17:00';
$data['form_location'] = BASE_URL . 'appointments/submit';
$this->view('appointment_form', $data);
}
public function submit(): void {
$this->validation->set_rules('appointment_time', 'appointment time', 'required|valid_time');
if ($this->validation->run() === true) {
$data['customer_name'] = post('customer_name', true);
$data['appointment_time'] = post('appointment_time', true);
$this->db->insert($data, 'appointments');
set_flashdata('Appointment booked successfully');
redirect('appointments/confirm');
} else {
$this->create();
}
}
View file:
<?php
echo form_open($form_location);
echo form_label('Appointment Time');
$attributes = [
'min' => $min_time,
'max' => $max_time,
'step' => 1800, // 30-minute intervals
'required' => true
];
echo form_time('appointment_time', $appointment_time, $attributes);
echo form_submit('submit', 'Book Appointment');
echo form_close();
?>
The example above shows how to restrict time selection to business hours with specific intervals. Users can only select times like 9:00, 9:30, 10:00, etc.
Server-side Validation Required: Always validate time inputs server-side with the valid_time rule. Browser validation can be bypassed by malicious users.
Example #4: Complete Store Hours Management
public function create(): void {
$update_id = segment(3, 'int');
if ($update_id > 0 && REQUEST_TYPE === 'GET') {
// Editing existing store hours
$record = $this->db->get_where($update_id, 'store_hours');
$data['day_name'] = $record->day_name;
$data['opening_time'] = $record->opening_time;
$data['closing_time'] = $record->closing_time;
} else {
// New entry or validation error
$data['day_name'] = post('day_name', true);
$data['opening_time'] = post('opening_time', true);
$data['closing_time'] = post('closing_time', true);
}
$data['form_location'] = BASE_URL . 'hours/submit/' . $update_id;
$this->view('hours_form', $data);
}
public function submit(): void {
$update_id = segment(3, 'int');
$this->validation->set_rules('day_name', 'day name', 'required');
$this->validation->set_rules('opening_time', 'opening time', 'required|valid_time');
$this->validation->set_rules('closing_time', 'closing time', 'required|valid_time|callback_closing_after_opening');
if ($this->validation->run() === true) {
$data['day_name'] = post('day_name', true);
$data['opening_time'] = post('opening_time', true);
$data['closing_time'] = post('closing_time', true);
if ($update_id > 0) {
$this->db->update($update_id, $data, 'store_hours');
set_flashdata('Store hours updated successfully');
} else {
$this->db->insert($data, 'store_hours');
set_flashdata('Store hours added successfully');
}
redirect('hours/manage');
} else {
$this->create();
}
}
public function callback_closing_after_opening($closing_time): string|bool {
$opening_time = post('opening_time', true);
if ($opening_time === '' || $closing_time === '') {
return true; // Let required rule handle empty values
}
if (strtotime($closing_time) <= strtotime($opening_time)) {
return 'The {label} must be after the opening time.';
}
return true;
}
View file:
<?php
echo form_open($form_location);
echo form_label('Day');
echo form_input('day_name', $day_name);
echo form_label('Opening Time');
$open_attrs = [
'step' => 900, // 15-minute intervals
'required' => true
];
echo form_time('opening_time', $opening_time, $open_attrs);
echo form_label('Closing Time');
echo form_time('closing_time', $closing_time, $open_attrs);
echo form_submit('submit', 'Save Hours');
echo form_close();
?>
The example above demonstrates a complete store hours management system. Notice the callback validation ensures closing time is after opening time, and segment(3, 'int') is properly type-cast for the update ID.
Database Compatibility: Use MySQL's TIME type for time fields. HTML5 time inputs submit values like "09:00" or "14:30:15" which MySQL accepts directly. If seconds are omitted, MySQL automatically stores them as :00.