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

ParameterTypeDescriptionDefaultRequired
$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

TypeDescription
stringThe generated HTML week input element

Understanding the Attributes Array

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

AttributeDescriptionExample 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

PHP
echo form_week('work_week');
HTML
Output: <input type="week" name="work_week">

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

PHP
public function create(): void {
    $data['work_week'] = post('work_week', true);
    
    // Default to current week if not submitted
    if ($data['work_week'] === '') {
        $data['work_week'] = date('Y') . '-W' . str_pad(date('W'), 2, '0', STR_PAD_LEFT);
    }
    
    $data['form_location'] = BASE_URL . 'timesheets/submit';
    $this->view('timesheet_form', $data);
}

View file:

View File
<?php
echo form_open($form_location);
echo form_label('Work Week');
echo form_week('work_week', $work_week);
echo form_submit('submit', 'Submit Timesheet');
echo form_close();
?>

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

PHP
public function timesheet(): void {
    $data['work_week'] = post('work_week', true);
    
    // Set constraints: can only submit timesheets for past 4 weeks
    $current_year = date('Y');
    $current_week = date('W');
    
    $data['min_week'] = $current_year . '-W' . str_pad(max(1, $current_week - 4), 2, '0', STR_PAD_LEFT);
    $data['max_week'] = $current_year . '-W' . str_pad($current_week, 2, '0', STR_PAD_LEFT);
    
    $data['form_location'] = BASE_URL . 'timesheets/submit';
    $this->view('timesheet_form', $data);
}

public function submit(): void {
    $this->validation->set_rules('work_week', 'work week', 'required|valid_week');
    $this->validation->set_rules('hours_worked', 'hours worked', 'required|numeric|greater_than[0]');
    
    if ($this->validation->run() === true) {
        $data['work_week'] = post('work_week', true);
        $data['hours_worked'] = post('hours_worked', true);
        $data['user_id'] = $_SESSION['user_id'];
        
        $this->db->insert($data, 'timesheets');
        set_flashdata('Timesheet submitted successfully');
        redirect('timesheets/manage');
    } else {
        $this->timesheet();
    }
}

View file:

View File
<?php
echo form_open($form_location);

echo form_label('Work Week');
$week_attrs = [
    'min' => $min_week,
    'max' => $max_week,
    'required' => true
];
echo form_week('work_week', $work_week, $week_attrs);

echo form_label('Hours Worked');
echo form_number('hours_worked', $hours_worked, ['required' => true, 'step' => 0.5, 'min' => 0]);

echo form_submit('submit', 'Submit Timesheet');
echo form_close();
?>

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

PHP
public function create(): void {
    $update_id = segment(3, 'int');
    
    if ($update_id > 0 && REQUEST_TYPE === 'GET') {
        // Editing existing record
        $record = $this->db->get_where($update_id, 'schedules');
        $data['task_name'] = $record->task_name;
        $data['schedule_week'] = $record->schedule_week; // Already in YYYY-W## format
    } else {
        // New record or validation error
        $data['task_name'] = post('task_name', true);
        $data['schedule_week'] = post('schedule_week', true);
    }
    
    $data['form_location'] = BASE_URL . 'schedules/submit/' . $update_id;
    $this->view('schedule_form', $data);
}

public function submit(): void {
    $update_id = segment(3, 'int');
    
    $this->validation->set_rules('task_name', 'task name', 'required');
    $this->validation->set_rules('schedule_week', 'schedule week', 'required|valid_week');
    
    if ($this->validation->run() === true) {
        $data['task_name'] = post('task_name', true);
        $data['schedule_week'] = post('schedule_week', true);
        
        if ($update_id > 0) {
            $this->db->update($update_id, $data, 'schedules');
            set_flashdata('Schedule updated successfully');
        } else {
            $this->db->insert($data, 'schedules');
            set_flashdata('Schedule created successfully');
        }
        
        redirect('schedules/manage');
    } else {
        $this->create();
    }
}

View file:

View File
<?php
echo form_open($form_location);

echo form_label('Task Name');
echo form_input('task_name', $task_name, ['required' => true]);

echo form_label('Schedule Week');
echo form_week('schedule_week', $schedule_week, ['required' => true]);

echo form_submit('submit', 'Save Schedule');
echo form_close();
?>

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:

2026-W51

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).