form_week()
function form_week(string $name, ?string $value = null, array $attributes = []): string
Description
Generates an HTML week input field with a native browser week picker. The input allows users to select a week and year, submitting data in YYYY-W## format (ISO 8601 week numbering).
Browser Display vs Submitted Format: Browsers display weeks according to user locale, but always submit in consistent ISO 8601 format (YYYY-W##) that your PHP code can rely on.
Demonstration
Below is the native HTML5 week input rendered by form_week():
Parameters
| Parameter | Type | Description | Default | Required |
|---|---|---|---|---|
| $name | string | The name attribute for the input element | N/A | Yes |
| $value | string|null | The week value in YYYY-W## format (zero-padded) | null | No |
| $attributes | array | Additional HTML attributes as key-value pairs | [] | No |
Return Value
| Type | Description |
|---|---|
| string | The generated HTML week input element |
Understanding the Attributes Array
The $attributes array accepts standard HTML input attributes. For week inputs, these are particularly useful:
| Attribute | Description | Example Value |
|---|---|---|
min |
Earliest selectable week (YYYY-W##) | '2025-W01' |
max |
Latest selectable week (YYYY-W##) | '2025-W52' |
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>
Week Format: Week numbers must be zero-padded: W01 not W1. January 1st might be in week 52 or 53 of the previous year according to ISO 8601 rules.
Example #1: Basic Week Input
The example above shows the simplest usage. This generates a week input where users can select any week using their browser's native week picker.
Example #2: Week Input with Default Current Week
View file:
The example above shows setting the current week as a default value. Note the use of str_pad() to ensure week numbers are zero-padded (W05 not W5).
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: Timesheet System with Constraints
View file:
The example above demonstrates a weekly timesheet system with business constraints. Users can only submit timesheets for the past 4 weeks, preventing submissions for future or very old weeks.
ISO 8601 Week Numbering: Week numbers follow the ISO 8601 standard where weeks start on Monday. Week 1 is the first week with a Thursday in the new year. This means January 1st might be in week 52 or 53 of the previous year.
Example #4: Complete Create/Update Pattern
View file:
The example above demonstrates the standard create/update pattern with a week field. No conversion is needed between the form and database because the YYYY-W## format can be stored directly in a VARCHAR column.
Database Storage: Store week values in a VARCHAR(8) column in YYYY-W## format. This keeps the data human-readable and makes it straightforward to query and filter by specific weeks.
Below is an example of a sample value that could be stored in a database column, representing week 51 of the year 2026:
Once again, the database column type - in this instance - would be VARCHAR(8). The database column name could be something like 'target_week' (or whatever you feel is appropriate for your use case).