Trongate PHP Framework Docs
Introduction
Quick Start
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
Security
Tips And Best Practices

Validating Date and Time Data

Trongate provides built-in validation rules for all HTML5 date and time input formats. These rules ensure data is in the correct format and represents valid dates and times.

Built-In Validation Rules

Rule Format Validated Example Use With
valid_date YYYY-MM-DD 2025-12-27 form_date()
valid_time HH:MM or HH:MM:SS 14:30 or 14:30:00 form_time()
valid_datetime_local YYYY-MM-DDTHH:MM 2025-12-27T14:30 form_datetime_local()
valid_month YYYY-MM 2025-12 form_month()
valid_week YYYY-W## 2025-W52 form_week()

Basic Usage

Validating a Date

Validating a Time

Validating a DateTime

Validating a Month

Validating a Week

What Each Rule Validates

valid_date

Validates that the value:

  • Matches the YYYY-MM-DD format exactly
  • Represents a valid calendar date
  • Properly handles leap years (Feb 29 only in leap years)
  • Rejects invalid dates like 2025-02-30 or 2025-13-01

valid_time

Validates that the value:

  • Matches HH:MM or HH:MM:SS format
  • Hours are between 00-23
  • Minutes are between 00-59
  • Seconds (if provided) are between 00-59

valid_datetime_local

Validates that the value:

  • Matches YYYY-MM-DDTHH:MM format exactly
  • Contains the literal "T" separator
  • Date portion is valid (same rules as valid_date)
  • Time portion is valid (same rules as valid_time)

valid_month

Validates that the value:

  • Matches YYYY-MM format exactly
  • Year is a 4-digit number
  • Month is between 01-12

valid_week

Validates that the value:

  • Matches YYYY-W## format exactly
  • Contains the literal "W" prefix for week number
  • Year is a 4-digit number
  • Week number is between 01-53

Combining with Other Rules

Date and time validation rules work seamlessly with other validation rules:

Optional Fields: If a date/time field is optional (not required), the validation rule will only check the format when a value is provided. An empty value will pass validation.

Complete Form Example

Custom Validation with Callbacks

For business logic validation beyond format checking, use custom callback methods:

Date Range Validation

Time Range Validation

Future Date Only

Past Date Only

Business Hours Validation

Weekday Only Validation

Date Within Range

Minimum Age Validation

DateTime Range Validation

Appointment Slot Validation

Validation Error Messages

Default error messages for built-in rules:

Rule Default Error Message
valid_date "The [field label] must be a valid date in YYYY-MM-DD format."
valid_time "The [field label] must be a valid time in HH:MM or HH:MM:SS format."
valid_datetime_local "The [field label] must be a valid datetime in YYYY-MM-DDTHH:MM format."
valid_month "The [field label] must be a valid month in YYYY-MM format."
valid_week "The [field label] must be a valid week in YYYY-W## format."

Real-World Validation Scenarios

Booking System

Project Timeline

Combining Multiple Validations

Important Notes

  • Built-in validation rules check format AND validity (e.g., valid_date rejects Feb 30)
  • Use callbacks for business logic validation (ranges, availability, etc.)
  • Empty values pass validation unless required is specified
  • Always return true or an error string from callbacks
  • Use {label} in error messages for automatic label substitution
  • Callbacks should handle empty values gracefully (let required rule handle them)
  • Browser validation is a convenience, not security - always validate server-side
  • Consider timezone implications when validating datetime values

Best Practice: Use built-in rules for format validation and custom callbacks for business logic. This separation keeps your code clean and maintainable.

We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.

Share your thoughts in the Documentation Feedback.

Leave Feedback About This Page