Laravel 5.3 date validator: equal to or after start_date
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
In developing your application using Laravel 5.3, you may encounter the need to validate start and end dates for events while considering specific date conditions. One scenario is when end_date needs to be either equal to start_date or after it. While the built-in rules seem inadequate at first, there are multiple solutions to accomplish this goal.
Using Custom Rule
Validator::extend allows you to create new validation rules that can be easily used with Laravel validators. Here's an example for our current scenario:
php
Validator::extend('equal_or_after', function ($attribute, $value, $parameters) {
// Check if the date is equal or after start_date
return (is_null($parameters[0]) || strcmp(date('Y-m-d', strtotime($value)), $parameters[0]) >= 0);
});php
$validator = Validator::make($data, [
'start_date' => 'required|date',
'end_date' => ['equal_or_after:start_date'],
]);Alternative Methods
Considering that validation is a fundamental part of Laravel's core functionality, there are several alternative ways to achieve the desired result without adding custom rules: 1. Split the logic into separate validators: - Create two independent validators for each date field. First validate start_date and then process end_date based on the results. - This approach is simple but not ideal in terms of code maintainability and testing. 2. Use third-party packages: - Several Laravel packages provide additional validation rules, such as Carbon'sCarbonRules or Gourmet's CustomValidation. These packages may already include the functionality you need for start and end date validations.
3. Use a combination of rules:
- Start by defining the rules for each field individually and then use the Laravel 5.3 sometimes() method to group them. Then, add conditional statements for checking specific scenarios (like equal or after date):
php
$validator = Validator::make($data, [
'start_date' => 'required|date',
]);
if ($validator->fails()) {
$rules = ['end_date' => 'after:start_date'];
} else {
$rules = ['end_date' => 'equal:start_date'];
}
$validator2 = Validator::make($data, $rules);