Trongate PHP Framework Docs
Introduction
Basic Concepts
Understanding Routing
Intercepting Requests
Module Fundamentals
Database Operations
Templates
Helpers
Form Handling
Form Validation
Working With Files
Image Manipulation
Working With Dates & Times
Language Control
Authorization & Authentication
Tips And Best Practices

Time Input Fields

The time input field allows users to select a time of day. It provides a user-friendly time picker and submits data in HH:MM or HH:MM:SS format.

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.

Basic Usage

Use to create a time input field:

Demonstration

Below is the native HTML5 time input rendered by form_time():

Function Signature

Parameters

Parameter Type Description Default
$name string The name attribute for the input (required) N/A
$value string|null The value in HH:MM or HH:MM:SS format null
$attributes array Additional HTML attributes []

With a Default Value

With Attributes

Output:

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

Downloadable Demo: Want to see the function in action? A fully working demo is just one click away! Check out the complete Room Bookings demo module example on GitHub:

https://github.com/trongate/Trongate-v2-Room-Bookings

This repository provides a ready-to-use example of building a room booking system using the Trongate PHP framework (version 2). It includes pagination, form validation, secure admin access, native HTML5 time input handling, custom time range validation, and clean separation of concerns.

Common Attributes

Attribute Purpose Example
min Earliest selectable time (HH:MM) ['min' => '09:00']
max Latest selectable time (HH:MM) ['max' => '17:00']
step Time interval in seconds ['step' => 900] (15 min)
required Field must have a value ['required' => true]
readonly Prevent user editing ['readonly' => true]
disabled Disable the input ['disabled' => true]

Understanding the Step Attribute

The step attribute controls the time interval in seconds:

Step Value Interval Example
60 1 minute 09:00, 09:01, 09:02...
300 5 minutes 09:00, 09:05, 09:10...
900 15 minutes 09:00, 09:15, 09:30...
1800 30 minutes 09:00, 09:30, 10:00...
3600 1 hour 09:00, 10:00, 11:00...
1 1 second 09:00:00, 09:00:01...

Default Step: If no step is specified, browsers typically default to 1-minute intervals (step="60").

Setting Current Time as Default

How to Repopulate Forms After Validation Errors

When a form fails validation, you need to redisplay it with the user's entered value:

Controller Example

View Example

Form Repopulation Pattern: Always use post('field_name', true) in the controller to fetch submitted values. The true parameter trims whitespace, ensuring clean data.

Real-World Example: Store Opening Hours

Controller:

View:

Working with the Create/Update Pattern

Type-Casting Segments: Always use segment(3, 'int') when expecting numeric IDs. This prevents type-related bugs and improves code clarity.

Database Storage

Time inputs submit in HH:MM or HH:MM:SS format, which maps to the MySQL TIME type:

Table Schema

Inserting Data

Direct Storage: HTML5 time inputs submit in formats that MySQL's TIME type accepts directly. If the user submits 09:00, MySQL automatically stores it as 09:00:00.

Formatting Times for Display

When displaying times to users, you may want different formats:

Validation

Use the valid_time validation rule to ensure the submitted value is in the correct format:

This validates that the value matches HH:MM or HH:MM:SS format and represents a valid time.

Client vs Server Validation: While browsers validate time formats client-side, always use server-side validation with valid_time. Client validation can be bypassed and doesn't protect your database.

See the Validating Date and Time Data chapter for complete validation rule documentation.

Business Hours Example

Here's a complete example for managing daily business hours:

Appointment Booking Example

Restricting time selection to business hours with 30-minute intervals:

Common Patterns

Office Hours (15-minute intervals)

Class Schedule (1-hour intervals)

Precise Time Entry (with seconds)

12-Hour vs 24-Hour Display

The browser automatically displays times in the user's preferred format:

  • US users: Typically see 12-hour format with AM/PM
  • European users: Typically see 24-hour format
  • Data submitted: Always in 24-hour format (HH:MM)

Best of Both Worlds: Users see time in their familiar format, but your application always receives consistent 24-hour format data. No conversion logic needed!

Browser Rendering

Different browsers provide different time picker interfaces:

  • Chrome/Edge: Dropdown with hour/minute selectors and AM/PM toggle (if 12-hour)
  • Firefox: Separate hour and minute input boxes with increment/decrement buttons
  • Safari: Native time picker wheel
  • Mobile: Native time picker optimized for the device

Important Notes

  • Times are always submitted in 24-hour format (HH:MM or HH:MM:SS)
  • The step attribute controls granularity in seconds
  • Default step is 60 seconds (1 minute) if not specified
  • Use MySQL's TIME type for time-only storage
  • Min/max attributes restrict the selectable time range
  • Browser validates format automatically, but server validation is still required
  • Display format (12h vs 24h) is determined by user's system locale

Value Format: The value attribute must be in HH:MM or HH:MM:SS format (24-hour). Values like 2:30 PM or 14.30 will not work and the field will appear empty.

Combining Date and Time

For applications that need both date and time, you have two options:

Option 1: Separate Fields

Option 2: Single datetime-local Field

Each approach has its uses - separate fields give more control, while datetime-local is simpler for the user.