form_datetime_local()

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

Description

Generates an HTML datetime-local input field with a native browser date and time picker. The input combines date and time selection in a single control and always submits in ISO 8601 format (YYYY-MM-DDTHH:MM).

Browser Display vs Submitted Format: Browsers display datetime-local inputs according to user locale, but always submit in consistent ISO 8601 format (YYYY-MM-DDTHH:MM) that your PHP code can rely on.

Demonstration

Below is the native HTML5 date and time input rendered by form_datetime_local():

The following helper functions handle bidirectional conversion between MySQL DATETIME values and HTML datetime-local inputs. Use them whenever loading values from the database or preparing form submissions for storage.

Parameters

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

Return Value

TypeDescription
stringThe generated HTML datetime-local input element

Understanding the Attributes Array

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

AttributeDescriptionExample Value
min Earliest selectable datetime (YYYY-MM-DDTHH:MM) '2025-01-01T09:00'
max Latest selectable datetime (YYYY-MM-DDTHH:MM) '2025-12-31T17: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]

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

Example #1: Basic DateTime-Local Input

View file:

Output:

Example #2: DateTime-Local Input with Value and Constraints

View file:

The example above demonstrates the standard Trongate pattern for form repopulation with datetime constraints. Users can only select datetimes within business hours next week, in 30-minute intervals.

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.

Example #3: Storing DateTime-Local in Database

The example above shows the required conversion when storing datetime-local values. The form submits 2025-12-27T14:30, which must be converted to 2025-12-27 14:30:00 for MySQL's DATETIME column.

Format Conversion: Replace the "T" with a space and append ":00" for seconds. This converts the ISO 8601 datetime-local format to MySQL's DATETIME format.

Example #4: Complete Create/Update Pattern with DateTime Conversion

The example above demonstrates bidirectional conversion: when loading from the database, remove seconds and replace space with "T". When saving to the database, replace "T" with space and add ":00".

Model Helper Methods: For applications with many datetime-local fields, consider creating reusable conversion methods in your model to keep controllers clean and conversions consistent.