Date format validation in Laravel
- Validating Date Formats in Laravel
- Basic Date Validation Rules
- Validating Dates With date_format
- Time And DateTime Validation
- Validating ISO 8601 Date Formats
- Conclusion
Validating Date Formats in Laravel
Validating date formats is essential when accepting user input that depends on accurate scheduling, calculations, or storage. Laravel provides clear, powerful validation rules for dates. Laravel developers often struggle to understand which rule to use, how strict validation should be, and how to enforce specific formats.
Basic Date Validation Rules
The date Rule
The date rule checks if the value can be parsed into a valid date by PHP’s strtotime function or Carbon. It does not enforce a particular format.
$request->validate([ 'start_date' => 'required|date',]);
This ensures the input is a valid date but does not restrict how it is formatted.
The date_format Rule
The date_format rule checks if the value exactly matches a specified format.
$request->validate([ 'start_date' => 'required|date_format:Y-m-d',]);
The value must match the format exactly.
Difference Between date And date_format
The date rule checks if the value is a real date. The date_format rule checks if the format matches. These rules serve different purposes.
Validating Dates With date_format
Single Format Example
$request->validate([ 'event_date' => 'required|date_format:d/m/Y',]);
Accepts only values like 25/01/2025.
Multiple Accepted Formats
Use comma-separated syntax to match one of the given formats.
$request->validate([ 'event_date' => 'required|date_format:d/m/Y,m/d/Y',]);
Laravel requires the value to match any of the formats in the date_format rule. In this case, it accepts both 25/01/2025 and 01/25/2025.
Common Format Patterns
Y - four digit year m - two digit month d - two digit day H - hours i - minutes s - seconds
Time And DateTime Validation
Validating Combined Date And Time
$request->validate([ 'scheduled_at' => 'required|date_format:Y-m-d H:i',]);
Validating Full Timestamps
$request->validate([ 'timestamp' => 'required|date_format:Y-m-d H:i:s',]);
Partial Datetime Inputs With required_with
$request->validate([ 'date' => 'required_with:time|date_format:Y-m-d', 'time' => 'required_with:date|date_format:H:i',]);
Validating ISO 8601 Date Formats
$request->validate([ 'published_at' => 'required|date_format:Y-m-d\TH:i:sP',]);
Conclusion
Use either date to check if the value is a real date or date_format to enforce strict formatting, not both.
Stay Updated.
I'll you email you as soon as new, fresh content is published.