form_month()

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

Description

Generates an HTML month input field with a native browser month picker. The input allows users to select a month and year, submitting 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.

Demonstration

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

Parameters

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

Return Value

TypeDescription
stringThe generated HTML month input element

Understanding the Attributes Array

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

AttributeDescriptionExample Value
min Earliest selectable month (YYYY-MM) '2025-01'
max Latest selectable month (YYYY-MM) '2025-12'
required Field must have a value true
readonly Prevent user editing true
disabled Disable the input true

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

Month Format: Months must be zero-padded: 01 for January, 12 for December. The format is always YYYY-MM.

Example #1: Basic Month Input

View file:

Output:

Example #2: Month Input with Default Current Month

View file:

The example above shows setting the current month as a default value using date('Y-m') which automatically zero-pads the month.

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: Financial Reporting System

View file:

The example above demonstrates a financial reporting system where users can only generate reports for the past 12 months. The DateTime object makes month calculations clean and reliable.

Example #4: Create/Update Pattern for Subscriptions

View file:

The example above demonstrates the standard create/update pattern with a month field. No conversion is needed between the form and database because the YYYY-MM format can be stored directly in a VARCHAR column.

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

Example #5: Budget Planning with Future Months

View file:

The example above demonstrates budget planning where users can create budgets for the next 12 months. The DateTime object makes month calculations straightforward.

Database Storage: Store month values in a VARCHAR(7) column in YYYY-MM format. This keeps the data human-readable and makes it easy to query by year or month using string functions or database date functions.