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

ParameterTypeDescriptionDefaultRequired
$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

TypeDescription
stringThe generated HTML time input element

Understanding the Attributes Array

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

AttributeDescriptionExample 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

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

View file:

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

View file:

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

View file:

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.