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

Month Input Fields

The month input field allows users to select a month and year. It provides a user-friendly month picker and submits data in YYYY-MM format.

Browser Display vs Submitted Format: Browsers display months according to user locale, but always submit in consistent YYYY-MM format that your PHP code can rely on.

Basic Usage

Use to create a month input field:

Demonstration

Below is the native HTML5 month input rendered by form_month():

Function Signature

Parameters

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

With a Default Value

Setting Current Month as Default

Automatic Zero-Padding: PHP's date('Y-m') automatically zero-pads the month, producing values like 2025-01 for January.

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? We've made a working demo that you can download. Check out the complete Monthly Reports demo module example on GitHub:

https://github.com/trongate/Trongate-v2-Monthly-Reports-Module

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

Common Attributes

Attribute Purpose Example
min Earliest selectable month (YYYY-MM) ['min' => '2025-01']
max Latest selectable month (YYYY-MM) ['max' => '2025-12']
required Field must have a value ['required' => true]
readonly Prevent user editing ['readonly' => true]
disabled Disable the input ['disabled' => true]

How to Repopulate Forms After Validation Errors

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: Monthly Report Selection

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

Month inputs submit in YYYY-MM format, which can be stored directly in a VARCHAR column:

Table Schema

Inserting Data

Direct Storage: The YYYY-MM format from HTML5 month inputs can be stored directly in a VARCHAR(7) column. No conversion is needed, and the data remains human-readable.

Converting Month to Date Range

To query data for a specific month, you'll often need to convert to a date range:

Querying by Month

Using Date Functions

Using Date Range

Formatting Months for Display

When displaying months to users, you'll often want to format them in a more readable way:

Validation

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

This validates that the value matches YYYY-MM format and represents a valid month.

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

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

Restricting Month Selection

Current Year Only

Last 12 Months

Future Months (Budget Planning)

Common Use Cases

  • Billing and subscriptions - Monthly billing cycles and recurring payments
  • Financial reports - Monthly revenue reports, P&L statements, expense tracking
  • Planning and budgets - Monthly budgets, forecasts, and financial planning
  • Performance reviews - Monthly KPIs, metrics, and performance tracking
  • Content calendars - Editorial planning and content scheduling by month
  • Inventory reports - Monthly stock levels and inventory analysis
  • Sales tracking - Monthly sales targets, quotas, and achievement
  • Employee records - Start months, anniversary months, review periods

Browser Rendering

Different browsers provide different month picker interfaces:

  • Chrome/Edge: Dropdown with month and year selectors
  • Firefox: Similar dropdown interface with clear navigation
  • Safari: Native month picker wheel optimized for the platform
  • Mobile: Native month picker optimized for touch input

Excellent Browser Support: Month inputs have excellent support across all modern browsers and mobile devices, providing a consistent user experience.

Important Notes

  • Value format is always YYYY-MM (e.g., 2025-12)
  • Store as VARCHAR(7) in database
  • No conversion needed between form and database
  • PHP's date('Y-m') automatically zero-pads months
  • Use for monthly periods, not specific dates within a month
  • Browser displays in user's locale but always submits in YYYY-MM format
  • Min/max attributes restrict the selectable month range
  • Browser validates format automatically, but server validation is still required

Value Format: The value attribute must be in YYYY-MM format. Values like 12/2025 or 2025-December will not work and the field will appear empty.

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